PHP uses the Array_slice function to get a random array or the first few data, array_slice a few
First of all, let's say basic grammar:
Array_slice (array $array, int $offset [, int $length [, bool $preserve _keys]])
Array_slice () returns a sequence in an array of arrays specified by the offset and length parameters.
If offset is non-negative, the sequence starts at this offset in the array. If offset is negative, the sequence starts at a distance from the end of the array.
If length is given and is positive, then there will be so many cells in the sequence. If length is given and is negative, the sequence terminates so far away from the end of the array. If omitted, the sequence starts at offset from the end of the array.
Note Array_slice () defaults to reset the array's keys. From PHP 5.0.2, you can change this behavior by setting Preserve_keys to TRUE.
$input = Array ("A", "B", "C", "D", "E"), $output = Array_slice ($input,); Returns the array starting with the subscript returns "C", "D", and "e" $output = Array_slice ($input,-,); Returns "D" $output = Array_slice ($input,,); Returns "A", "B", and "C"//note the differences in the array keysprint_r (Array_slice ($input,,-));p Rint_r (array_slice ($input,,-, true));
If you need to return a random few data, you can first use Shuffle ($input), disrupt the original array, and then use Array_slice to get
ps:php Array_slice () function
Definition and usage
The Array_slice () function takes a value out of the array and returns it as a condition.
Note: If the array has a string key, the returned array retains the key name. (see Example 4)
Grammar
Copy the Code code as follows:
Array_slice (Array,offset,length,preserve)
parameters |
description |
array |
required. Specifies the input array. |
offset |
required. Numerical. Specifies the starting position of the element to be removed. from the back forward. Span style= "Border-bottom:windowtext 1pt; Border-left:windowtext 1pt; padding-bottom:0cm; padding-left:0cm; padding-right:0cm; Color:black; Border-top:windowtext 1pt; Border-right:windowtext 1pt; padding-top:0cm ">offset |
length |
Optional. Numerical. Specifies the length of the returned array. if length is positive, the number of elements is returned. if length is negative, the sequence terminates so far away from the end of the array. If omitted, the sequence will be style= border-bottom: windowtext 1pt; Border-left:windowtext 1pt; padding-bottom:0cm; padding-left:0cm; padding-right:0cm; Color:black; Border-top:windowtext 1pt; Border-right:windowtext 1pt; padding-top:0cm ">offset start until array |
preserve |
Optional. Possible values: • True-reserved key · False- default -Reset key |
Example 1
<?php$a=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");p Rint_r (Array_slice ($a,));? >
Output:
Copy the Code code as follows:
Array ([0] = Cat [1] = Horse)
Example 2
With a negative offset parameter:
<?php$a=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");p Rint_r (Array_slice ($a, -2,1));? >
Output:
Copy the Code code as follows:
Array ([0] = Horse)
Example 3
The preserve parameter is set to true:
<?php$a=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");p Rint_r (Array_slice ($a, 1,2,true));? >
Output:
Copy the Code code as follows:
Array ([1] = Cat [2] = Horse)
Example 4
With string key:
<?php$a=array ("A" and "Dog", "B" and "Cat", "c" = "Horse", "D" and "Bird");p Rint_r (Array_slice ($a,));? >
Output:
Copy the Code code as follows:
Array ([b] = Cat [c] = Horse)
The above is this article to introduce PHP using the Array_slice function to get an array of random or previous data, I hope you like.
http://www.bkjia.com/PHPjc/1058157.html www.bkjia.com true http://www.bkjia.com/PHPjc/1058157.html techarticle PHP uses the Array_slice function to get a random array or the first few data, array_slice a few first give you the basic syntax: Array_slice (array $array, int $offset [, int $length [, b Oo ...