PHP deletes the element ____php the specified subscript in the array

Source: Internet
Author: User

1, the preceding has said the array as the stack and the queue when the deletion element operations, that is, in order to delete the rules. So, what if you need to remove an element from the middle of an array? We need the unset () function We're going to talk about today.

2. The unset () function allows an element in an array to be canceled, but the array does not rebuild the index, which keeps the original index, because the index in PHP has a special meaning.

3, sample display:

<?php
    $arr = Array (1=> ' one ',2=> ' two ',3=> ' three ');
     
    Delete the element unset with subscript 2
   ($arr [2]);  will get Array (1=> ' one ',3=> ' three ')

    //Use Array_values () to re-establish the index
    $aar = array_values ($arr);  $aar = Array (0=> ' one ',1=> ' three ')
?>
4, the last code in the above example is to re-establish the array index, here I explain the following: Because after using the unset () function to delete an element, there is no way to re-establish the index subscript order. If you need a sequential index subscript, you can use the Array_values () function to recreate the index subscript order.

Note: The re-indexing of the premises says: Re-establish a subscript in the order of 0, even if your index is not named numerically, it will be indexed again.

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.