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