In a previous article we introduced how to add elements to the headers and tails of a PHP array since there are elements to add, then there is the deletion element, and today this article details how to delete the head and tail elements in the array, as well as any array elements.
Delete End element: Array_pop
Just like a bullet's magazine, the Last bullet that pops up is the first to pop up, in computer terminology, we call it the advanced post-stack.
Since Array_push is adding elements to the end of the array, Array_pop removes an element from the end of the array.
The Array_pop () function gets and returns the last element of the array, reducing the length of the entire array by 1, and if the array is empty (or not an array), then NULL is returned.
The syntax format is as follows:
Mixed Array_pop (array & $array)
The parameter array is an input array.
The following is an example of the last element of the Array_pop () function to delete the number of groups, as follows:
<?phpheader ("content-type:text/html; Charset=utf-8 "); $atr = Array (" PHP "," JAVA "," ASP "," VB "); Defines the array $array = Array_pop ($ATR); Delete the last element in the array echo "the deleted element is; $array <br> "; Outputs the deleted element Print_r ($ATR); Print array Structure?>
The output is:
Delete Head element: Array_shift
In addition to advanced after the outside, there is also a computer called Advanced first out stack, can be seen as a pipe, first go into the element first out
Since array_pop can pop the element from the end of the array, there should be a function that pops the element from the head of the array, and we use Array_shift
Mixed Array_shift (array & $array)
Let's use the Array_shift () function to delete an instance of the head element in the array, as follows:
<?phpheader ("content-type:text/html; Charset=utf-8 "); $atr = Array (" PHP "," JAVA "," ASP "," VB "); Defines the array $array = Array_shift ($ATR); Delete the first element in the array echo "the element being deleted is; $array <br> "; Outputs the deleted element Print_r ($ATR); Print array Structure?>
The output is:
In fact, the deletion of the number of the head element and the deletion of the tail element of the syntax is the same, but the function is not the same!
Remove elements from any array
Above we introduced how to delete the array of elements in the head and tail, delete any array of elements than the first two is simple, delete any element can be directly used unset on the line!
Here we use the unset function to implement deleting an instance of an element in any array, as follows:
<?phpheader ("content-type:text/html; Charset=utf-8 "); $atr = Array (" PHP "," JAVA "," ASP "," VB "); Defines the array unset ($atr [1]); Delete Javaprint_r ($ATR); Print array Structure?>
The output is:
Note: The above is an indexed array, the index array can be deleted by the specified subscript, the associated array delete elements, you can follow the key to delete the specified element!
The following example is how to delete the elements in the associative array , the specific code is as follows;
<?phpheader ("content-type:text/html; Charset=utf-8 "); $atr = Array (" name "=" topic.alibabacloud.com "," URL "=" www.php.cn "," QQ "=" 88526 "); Defines the array unset ($atr ["QQ"]); Delete 88526print_r ($ATR); Print array Structure?>
The output is:
Description
unset can delete any variable or array
The reference code is as follows:
<?phpheader ("content-type:text/html; Charset=utf-8 "); $atr = Array (" name "=" topic.alibabacloud.com "," URL "=" www.php.cn "," QQ "=" 88526 "); Defines the array unset ($ATR); Delete array var_dump ($ATR); Print array Structure?>
The output is:
In the next article, we'll show you how to delete duplicate elements in a PHP array!
"Related tutorials Recommended"
1. Related topics: "PHP array"
2. Related Video Course recommendation: " using arrays to implement stack operations: Array_push and Array_pop"