PHP deletes an array of elements specified in the subscript method PHP instance

Source: Internet
Author: User
The following small series for you to share a PHP delete array in the specified subscript element method, has a good reference value, I hope to be helpful to everyone. Let's take a look at it with a little knitting.

1, before the array as the stack and the queue to delete the element operation, that is, in order to delete the regular. So, what if you need to remove an element from the middle of the array? You need the unset () function We're going to talk about today.

2, the unset () function allows you to cancel an element in an array, but the array does not rebuild the index, that is, keep the original index, because the index in PHP has a special meaning.

3. Example shows:

<?php   $arr = Array (1=> ' one ',2=> ', ',3=> ' three ');       Delete the element labeled 2   unset ($arr [2]);//will get Array (1=> ' one ',3=> ' three ')    //Use Array_values () to re-index   $aar = Array_values ($arr); $aar = Array (0=> ' one ',1=> ' three ')?>

4, the last sentence in the above example code to re-establish the array index, here I explain the following: Because after using the unset () function to delete an element, and did not re-establish 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 refers to the re-establishment of an index starting at 0, even if your index is not numerically named, and is re-indexed.

This PHP delete array in the specified subscript element method is the small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support PHP Chinese network.

Articles you may be interested in:

PHP technique for parsing XML and generating SQL statements

PHP implementation of the regular expression grouping capture operation example PHP tips

PHP implementation of QQ login principle and implementation process PHP instance

Related Article

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.