Function for deleting the first element and last element of the array in php. php array _ PHP Tutorial

Source: Internet
Author: User
Function for deleting the first and last elements of the array in php, which is a php array. Php deletes the first element and the last element of an array. for a php array, how does one delete the first or last element of the array? In fact, this php deletes the first element and the last element of the array function, php array

How can I delete the first or last element of a php array? In fact, these two processes can be completed through the built-in functions array_pop and array_shift in php. The following describes how to operate them.

(1) use array_pop to delete the last element of the array, for example:

$user=array('apple','banana','orange');$result=array_pop($user);print_r($result);print_r($user);

The result will be:

Orange
Array ('apple', 'banana ')

(2) use array_shift to delete the first element of the array, for example:

$user=array('apple','banana','orange');$result=array_shift($user);print_r($result);print_r($user);

The result will be:

Apple
Array ('banana ', 'Orange ')

In fact, you can use the array_splice function to delete the first element of the array, that is:

The code is as follows:
$ User = array_splice ($ user, 1); // delete the first element of the array. Note that the returned element is the new array after deletion.

The following is a brief explanation of array_pop and array_shift:

Array_pop () pops up, returns the last unit of the array, and deletes the length of the array by one. If array is NULL (or not an array), NULL is returned.

Array_shift () removes the first unit of array and returns the result. This removes the length of array and moves all other units one bit forward. The names of all numeric keys are counted from scratch, and the names of text keys remain unchanged. If array is NULL (or not an array), NULL is returned.

For a php array, how does one delete the first or last element of the array? Actually this...

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.