Re-index of elements in PHP array, 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.
$arr = Array (' A ', ' B ', ' C ', ' d ');
Unset ($arr [1]);
Print_r ($arr);
?>
Print_r ($arr)
After that, the result is not that, and 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 ():
$arr = Array (' A ', ' B ', ' C ', ' d ');
Array_splice ($arr, 1, 1);
Print_r ($arr);
?>
Print_r ($arr), the result is a (www.111cn.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
$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.
From PHP tutorial Web: http://www.111cn.net/phper/php-cy/66372.htm
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 ']);
PHP deletes the elements in an array
Unset ($arr [1]);
http://www.bkjia.com/PHPjc/878813.html www.bkjia.com true http://www.bkjia.com/PHPjc/878813.html techarticle PHP Array To remove elements of the re-index, PHP array element Index if you want to delete an element in an array, you can directly use the unset, but what I see today is a surprise to me ...