Deletes the PHP array header, tail, and any element implementation code.

Source: Internet
Author: User

Deletes the PHP array header, tail, and any element implementation code.

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 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.

Sincearray_pushIs to add an element to the end of the array, then array_pop is to delete an element from the end of the array.

array_pop()The 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:

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ recognition = array ("PHP", "JAVA", "ASP", "VB "); // define the array $ array = array_pop ($ ASD); // Delete the last element of the array echo "the deleted element is; $ array <br> "; // output the deleted element print_r ($ ASD); // print the Array Structure?>

Output result:

Delete Header element:array_shift

In addition to advanced and outgoing operations, there is also an advanced, first-in-first-out stack in the computer, which can be seen as a pipeline. The advanced elements come out first.

Since array_pop can pop up elements from the end of the array, there should be a function that can pop up elements from the array header. We use array_shift

mixed array_shift ( array &$array )

Next we will use the array_shift () function to delete the instance of the Header element in the array. The specific code is as follows:

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ recognition = array ("PHP", "JAVA", "ASP", "VB "); // define the array $ array = array_shift ($ ASD); // Delete the first element in the array echo "the deleted element is; $ array <br> "; // 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:

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ recognition = array ("PHP", "JAVA", "ASP", "VB "); // define the array unset ($ recognition [1]); // Delete JAVAprint_r ($ recognition); // print the Array Structure?>

Output result:

Note: The index array is deleted. The index array can be deleted according to the specified subscript. The elements in the associated array can be deleted based on the key!

The following example shows how to delete the elements in the joined array. The Code is as follows;

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ recognition = array ("name" => "PHP 文 ", "URL" => "www.php.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:

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ recognition = array ("name" => "PHP 文 ", "URL" => "www.php.cn", "QQ" => "88526"); // defines the array unset ($ recognition); // deletes the array var_dump ($ recognition ); // print the Array Structure?>

Output result:

The next article describes how to delete repeated elements in a PHP array.

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

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.