Home >Backend Development >PHP Tutorial >How to re-index deleted elements in php array, php array element index_PHP tutorial
If you want to delete an element in an array, you can directly use unset, but what I saw today surprised me
So how can the missing elements be filled and the array re-indexed? The answer is
array_splice():
After print_r($arr), the result is A(www.jb51.net)rray ( [0] => a [1] => c [2] => d )
Delete the specified element from the array
array_search() is more practical
The array_search() function is the same as in_array(), searching for a key value in the array. If the value is found, the key of the matching element is returned. If not found, return false
But if you want to re-index the array, you need to use foreach to traverse the deleted array and then re-create an array. This is also possible.
array_values() returns all the values in the input array and indexes them numerically.
array_value($arr['date']);
Solution 1: Create a new array:
$newArr = array();
foreach($arr as $key=>$value){
$newArr[] = $value ;
}
Solution 2: Sort
sort($arr);