How to delete the header, tail, and any element in the PHP array

Source: Internet
Author: User
In the previous article, we introduced "how to add elements to the header and tail of a PHP array". since an element is added, an element is deleted, this article describes how to delete header and tail elements in an array, as well as any array elements. In the previous article, we introduced "how to add elements to the header and tail of a PHP array". since an element is added, an element is deleted, this article describes how to delete header and tail elements in an array, as well as any array elements.

Delete the end element: array_pop

Just like a bullet clip, the final bullet is the first to pop up. in computer terms, we call it an advanced post-release stack.

Since array_push adds an element to the end of the array, array_pop deletes an element from the end of the array.

The array_pop () function obtains and returns the last element of the array, and reduces the length of the entire array by 1. if the array is empty (or not an array), null is returned.

The syntax format is as follows:

mixed array_pop ( array &$array )

The parameter array is the input array.

The following is an example of the last element of the number of groups deleted by the array_pop () function. the code is as follows:

 "; // Output the deleted element print_r ($ ASD); // Print the array structure?>

Output result:

 "; // Output the deleted element print_r ($ ASD); // Print the array structure?>

Output result:

In fact, the syntax for deleting the header element and the tail element in the number of groups is the same, but the functions used are different!

Delete elements from any array

The preceding section describes how to delete the header and tail elements in an array. deleting any element in an array is easier than deleting the first two elements. you can simply use unset to delete any element!

The unset function is used to delete element instances in any array. the code is as follows:

 

Output result:

Note:The index array is deleted above. The index array can be deleted by the specified subscript, and the elements of the associated array can be deleted by the key!

The following example shows howDeletes an element from an associated array.The code is as follows;

 "PHP 文"", "URL" => "www.php1.cn", "QQ" => "88526"); // defines the array unset ($ recognition ["QQ"]); // delete 88526print_r ($ recognition); // Print the array structure?>

Output result:

Description

Unset can delete arbitrary variables or arrays.

The reference code is as follows:

 "PHP 文"", "URL" => "www.php1.cn", "QQ" => "88526"); // defines the array unset ($ recognition ); // delete the array var_dump ($ ASD); // Print the array structure?>

Output result:

[Recommended tutorials]

1. related topic recommendations: php Array (Array)

2. related video course recommendations: Using arrays for stack operations: array_push and array_pop

The preceding figure shows how to delete the header, tail, and details of any element in the PHP array. For more information, see other related articles in the first PHP community!

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.