PHP array operation, delete first and last element

Source: Internet
Author: User

for a PHP array, how do you delete the first element or the last element of the array? In fact, both of these processes can be done through PHP's own functions Array_pop and Array_shift, the following is a detailed description of how to operate. (1use Array_pop to delete the last element of the array, for example: $user=array ('Apple','Banana','Orange'); $result=Array_pop ($user);p rint_r ($result);p rint_r ($user); The result is: Orangearray ('Apple','Banana') (2use Array_shift to delete the first element of the array, for example: View Code printing $user=array ('Apple','Banana','Orange'); $result=Array_shift ($user);p rint_r ($result);p rint_r ($user); The result is: Applearray ('Banana','Orange'in fact, deleting the first element of the array can also use the Array_splice function, namely: $user=array_splice ($user,1);//Delete the first element of the array, and note that the new array is returned when it is deletedHere's a simple explanation for Array_pop and Array_shift: Array_pop () pops up and returns the last cell of the array, minus one of the array's length. If array is empty (or not an array), NULL is returned. Array_shift () Moves the first cell of the array out and returns as a result, reducing the length of the array and moving all other cells forward one bit. All numeric key names are changed from zero to start, and the text key name is unchanged. If the array is empty (or not an array), NULL is returned. 

array: Offset: Specifies the starting position of the removed element. If it is a positive number, start with the previous one, and if it is negative, take the offset from the back to the absolute value. The first element of the index array in PHP is very easy to take, subscript 0 can be, not much explanation. Focus on sharing PHP with the first method of associative array. First put the code I wrote today: The code is as follows//take the default first channel name$channel _arr = $ This->get_from_channel ();//All channel Arrays$arr _num =count ($channel _arr); $first _channel= Array_slice ($channel _arr,0,-($arr _num-1));//Fetch Channel Array first$html ['From_channel'] = $first _channel[0]; Delete the array code as follows<?php$a=array (0="Dog",1="Cat",2="Horse",3="Bird");p Rint_r (Array_slice ($a,1,2));?>output Array ([0] = Cat [1] =Horse) also has the Array_shift () function to delete the first element in the array and return the value of the deleted element. The relative Array_pop () function deletes the last element in the array. Several functions are used down to feel Array_search () The comparison utility Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. If it is not found, it returnsfalseThe code below copies the code $array= Array ('1','2','3','4','5'); $del _value=3; unset ($array [Array_search ($del _value, $array)]);//Delete this element using unsetPrint_r ($array); output Array ('1','2','4','5'); Removing an element from an array in PHP removes an array element in PHP in the following ways: To delete an element, copy the code with the onset () code as follows unset ($array [3]); unset ($array ['Foo']); To delete multiple discontinuous elements, also use the unset () code to copy the code unset ($array [3] $array [5]); unset ($array ['Foo'] $array ['Bar']); To delete multiple contiguous elements, use the Array_splice () code as follows to copy the code array_splice ($array $offset $length);

PHP Array_shift Deletes the first value in the array: Define and use the Array_shift () function to delete the first element in the array and return the value of the deleted element. Note: If the key is numeric, all elements will get a new key from0To begin, and to1Increasing. (see Example2). The syntax Array_shift (array) parameter describes the array required. Specifies the input array. Example1<?php$a=array ("a"="Dog","b"="Cat","C"="Horse"); Echo array_shift ($a);p rint_r ($a);?>output: Dogarray ([b]Cat [c] = =Horse) Example2with number keys:<?php$a=array (0="Dog",1="Cat",2="Horse"); Echo array_shift ($a);p rint_r ($a);?>output: Dogarray ([0] = Cat [1] = Horse)

PHP array operation, delete first and last element

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.