PHP array_slice () function definition and usage

Source: Internet
Author: User
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

Array_slice (Array,start,length,preserve)

Parameter description

Array must be. Specifies the array.

Start must have. Numerical. Specifies the starting position of the element to be removed. 0 = first element.

If the value is set to a positive number, it is taken from the previous start.

If the value is set to a negative number, the start absolute value is taken from the back forward. 2 means starting with the second-to-last element of the array.

length Optional. Numerical. Specifies the length of the returned array.

If the value is set to an integer, the number of elements is returned.

If the value is set to a negative number, the function terminates the fetch at such a distance from the end of the example array.

If the value is not set, returns all elements starting at the position set by the start parameter until the end of the array.

Preserve Optional. Specifies whether the function retains the key name or resets the key name. Possible values:

True-reserved Key name

False-Default. Reset Key Name

return Value: returns the selected part of the array.

Example 1:

<! DOCTYPE Html>

Operation Result:

Array ([0] = green [1] = blue)

Example 2:

Use the negative start parameter:

<! DOCTYPE Html>

Operation Result:

Array ([0] = yellow)

Example 3:

Set the Preserve parameter to true:

<! DOCTYPE Html>

Operation Result:

Array ([1] = green [2] = blue)

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.