This article describes how to use the array_slice function in php to obtain random arrays or the first few pieces of data. For more information, see
This article describes how to use the array_slice function in php to obtain random arrays or the first few pieces of data. For more information, see
Let's talk about the basic syntax:
Array_slice (array $ array, int $ offset [, int $ length [, bool $ preserve_keys])
Array_slice () returns a sequence in the array specified by the offset and length parameters.
If offset is not negative, the sequence starts from this offset in array. If offset is negative, the sequence starts from the end of the array.
If length is given and is positive, the sequence will have so many units. If length is given and it is negative, the sequence is terminated so far from the end of the array. If this parameter is omitted, the sequence starts from offset to the end of array.
Note that array_slice () resets the array key by default. Since 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 returns "c", "d", and "e" $ output = array_slice ($ input,-,) starting with the subscript ,-,); // returns "d" $ output = array_slice ($ input,); // returns "a", "B ", and "c" // note the differences in the array keysprint_r (array_slice ($ input,-); print_r (array_slice ($ input,-, true ));
To return a random number of data records, shuffle ($ input) and array_slice can be used to query the original array.
PS: PHP array_slice () function
Definition and usage
The array_slice () function extracts a value from the Array Based on the Conditions and returns it.
Note: If the array has a string key, the returned array retains the key name. (See example 4)
Syntax
The Code is as follows:
Array_slice (array, offset, length, preserve)
Parameters
Description
Array
Required. Specifies the input array.
Offset
Required. Value. Specifies the start position of the retrieved element.
If it is a positive number, it is taken from the beginning to the end. If it is a negative value, it is taken from the back to the absolute value of the offset.
Length
Optional. Value. Specifies the length of the returned array.
If the length is positive, the number of elements is returned.
If length is negative, the sequence ends so far from the end of the array.
If this parameter is omitted, the sequence starts from offset to the end of array.
Preserve
Optional. Possible values:
· True-reserved key
Example 1
<? Php $ a = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird "); print_r (array_slice ($ a, 1, 2);?>
Output:
The Code is as follows:
Array ([0] => Cat [1] => Horse)
Example 2
Offset parameter with negative values:
<? Php $ a = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird "); print_r (array_slice ($ a,-2, 1);?>
Output:
The Code is as follows:
Array ([0] => Horse)
Example 3
Set the preserve parameter to true:
<? Php $ a = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird "); print_r (array_slice ($ a, 1, 2, true);?>
Output:
The Code is as follows:
Array ([1] => Cat [2] => Horse)
Example 4
String key:
<? Php $ a = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ", "d" => "Bird"); print_r (array_slice ($ a, 1, 2);?>
Output:
The Code is as follows:
Array ([B] => Cat [c] => Horse)
The above section describes how to use the array_slice function in php to obtain random arrays or the first few pieces of data.
,