PHP array to remove elements of the re-index method, PHP array element index _php tutorial

Source: Internet
Author: User

PHP array to remove elements of the re-index method, PHP array element index


If you want to delete an element in an array, you can use the unset directly, but what you see today is a surprise to me.

Copy the Code code as follows:
<?php
$arr = Array (' A ', ' B ', ' C ', ' d ');
Unset ($arr [1]);
Print_r ($arr);
?>

Print_r ($arr), the result is not that, the end result is an Array ([0] = a [2] = c [3] + D)

So how can the missing elements be filled and the array will be re-indexed? The answer is

Array_splice ():

Copy the Code code as follows:
<?php
$arr = Array (' A ', ' B ', ' C ', ' d ');
Array_splice ($arr, 1, 1);
Print_r ($arr);
?>

Print_r ($arr), the result is a (www.jb51.net) Rray ([0] = a [1] = + c [2] + D)

To delete an array-specified element

Array_search () more practical

The Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns false if not found
Copy the Code code as follows:
$array = Array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ');
$del _value = 3;
Unset ($array [Array_search ($del _value, $array)]);//use unset to delete this element
Print_r ($array);

Output
Array (' 1 ', ' 2 ', ' 4 ', ' 5 ');

But this is also possible if you want to re-index an array and need to re-establish an array after using the foreach traversal of the deleted array.


How to delete an array key in PHP to make it an indexed array

Array_values () returns all the values in the input array and establishes a numeric index on them.

Array_value ($arr [' Date ']);

After PHP deletes an item specified in a two-dimensional array, the index is still in order?

Solution One: Create a new array:

$NEWARR = Array ();

foreach ($arr as $key = = $value) {
$NEWARR [] = $value;

}

Solution Two: Sort
Sort ($arr);

http://www.bkjia.com/PHPjc/879723.html www.bkjia.com true http://www.bkjia.com/PHPjc/879723.html techarticle PHP Array To remove elements of the re-index method, PHP array element Index if you want to delete an element in an array, you can directly use the unset, but the things I see today make me big ...

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