PHP uses the Array_slice function to get a random array or several previous data _php instances

Source: Internet
Author: User
Tags terminates

First of all, the basic grammar:

Array_slice (array $array, int $offset [, int $length [, bool $preserve _keys]])

Array_slice () returns a sequence of arrays in the array 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 will start at a distance 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 terminates at such a distance from the end of the array. If omitted, the sequence will start at offset until the end of the array.

Note Array_slice () resets the key of the array by default. 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 ($in Put,,,); Returns "A", "B", and "C"
//differences in the array keys
print_r (Array_slice ($input,,-));
Print_r (Array_slice ($input,,-, true));

If you need to return a random number of data, you can use Shuffle ($input), and then scramble the original array, then use Array_slice to get

ps:php Array_slice () function

Definitions and usage

The Array_slice () function extracts a value from the array, and returns it.

Note: If the array has a string key, the returned array retains the key name. (see Example 4)

Grammar

Copy Code code as follows:

Array_slice (Array,offset,length,preserve)

Parameters
Describe
Array
Necessary. A specified array of inputs.
Offset
Necessary. Numerical. Specify the starting position of the element to be removed.
if it is a positive number, then start from the back and, if it is negative, move forward Offset absolute value.
Length
Optional. Numerical. Specify the length of the array to be returned.
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 is set from the Offset start until Array the end.
Preserve
Optional. Possible values:
· true-Preserve 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));
? >

Output:

Copy 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");
Print_r (Array_slice ($a, -2,1));
? >

Output:

Copy 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");
Print_r (Array_slice ($a, 1,2,true));
? >

Output:

Copy Code code as follows:

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:

Copy Code code as follows:

Array ([b] => Cat [c] => horse)

The above is this article to introduce you to PHP using the Array_slice function to get an array of random or the first few data, I hope you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.