In the previous article, we introduced 4 methods of inserting new elements into the array. If you are interested, you can click on the link to view → "PHP Array Learning Tips to Use Functions to Insert Elements (1)" , "PHP Array Learning Skills Using Functions to Insert Elements (2)". This time we will show you how to use PHP to delete array elements. You can refer to it if you need it.
Delete if you add it. Since we introduced adding array elements before, this article will introduce deleting elements, mainly to learn how to delete elements at the beginning and end of the array. First, let's take a look at how to delete the front (head) element of the array.
Let’s take a look at the following example:
Output result:
You can see this example , there were 5 elements in our original $arr array. After using thearray_shift($arr)
method, we reusedvar_dump($arr)
to output the array and found that there were only 4 elements. , the array head element is deleted.
array_shift($arr)
After the function deletes the first element at the beginning of the $arr array, the length of the arr array will be reduced by 1 and all other elements will be moved forward by one . If the key is numeric, all elements will get a new key, starting at 0 and increasing by 1; but string keys will remain unchanged.
And after we perform the deletion operation, we output the original array to observe whether the elements are deleted. It is not difficult to find thatarray_shift() will change theof the original array.
Okay, we have introduced the method of deleting the head element of the array. Now let’s learn about the method of deleting the tail element of the array.
Output result:
It can be seen that there are 5 elements in our original $arr array. After usingarray_pop($arr )
After the method, usevar_dump($arr)
to output the array again, and find that there are only 4 elements, and the tail element of the array has been deleted.
array_pop($arr)
After the function deletes the last element of the $arr array, the length of the arr array will be reduced by 1. Andarray_pop() will also change the original array.
Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial
Finally, I would like to recommend a free video tutorial on PHP arrays:PHP function array array function video explanation, come and learn!
The above is the detailed content of PHP array learning how to remove the first and last values . For more information, please follow other related articles on the PHP Chinese website!