Home>

Let me give you the basic syntax:

array_slice (array $array, int $offset [, int $length [, bool $preserve_keys]])

array_slice () Returns a sequence from the array array specified by the offset and length parameters.

If offset is non-negative, the sequence will start at this offset in the array.If offset is negative, the sequence will start so far from the end of the array.

If length is given and is positive,Then there will be so many units in the sequence.If length is given and is negative,The sequence will end so far from the end of the array.If omitted,The sequence will start at offset and continue to the end of the array.

Note that array_slice () resets the keys of the array by default.As of php 5.0.2, this behavior can be changed by setting preserve_keys to true.

$input=array ("a", "b", "c", "d", "e");
$output=array_slice ($input,);//returns the array starting with the index 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 keys
print_r (array_slice ($input,,-));
print_r (array_slice ($input,,-, true));

If you need to return a few random data,You can use shuffle ($input);to shuffle the original array first,Then use array_slice to get

ps:php array_slice () function

Definition and usage

The array_slice () function fetches a piece of value from the array according to the condition.And return.

Note:If the array has string keys,The returned array will retain the key names.(See example 4)

grammar

array_slice (array, offset, length, preserve)

parameter
description
array
essential. Specifies the input array.
offset
essential. Value. Specifies the start position of the fetched element.
If it is positive,Then pick it up after you go,If negative,Take the absolute value of offset from back to front.
length
Optional. Value. Specifies the length of the returned array.
If length is positive, the number of elements is returned.
If length is negative, the sequence will terminate so far from the end of the array.
If omitted,The sequence will start at offset and go to the end of the array.
preserve
Optional. Possible values:
True-reserved keys
False-default-reset key

Example 1

<?php
$a=array (0 =>"dog", 1 =>"cat", 2 =>"horse", 3 =>"bird");
print_r (array_slice ($a, 1,2));
?>
</pre>
</div>
<p>
Output:
</p>
<pre>
<code>
array ([0] =>cat [1] =>horse)

Example 2

With a negative offset parameter:

<?php$a=array (0 =>"dog", 1 =>"cat", 2 =>"horse", 3 =>"bird");print_r (array_slice ($a, -2,1));?>

Output:

array ([0] =>horse)

Example 3

The preserve parameter is set to true:

<?php
$a=array (0 =>"dog", 1 =>"cat", 2 =>"horse", 3 =>"bird");
print_r (array_slice ($a, 1,2, true));
?>

Output:

array ([1] =>cat [2] =>horse)

Example 4

With string key:

<?php$a=array ("a" =>"dog", "b" =>"cat", "c" =>"horse", "d" =>"bird");print_r (array_slice ($a, 1,2));?>

Output:

array ([b] =>cat [c] =>horse)
php
  • Previous Summary of js implementation to create and delete html elements
  • Next Basic grammar of JSP study notes