Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove Array Element and Then Re-Index Array?

People also ask

How do I reindex an array?

The re-index of an array can be done by using some inbuilt function together. These functions are: array_combine() Function: The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values.

How do you remove an element from an array and a shift index?

To remove an array element at index i, you shift elements with index greater than i left (or down) by one element. For example, if you want to remove element 3, you copy element 4 to element 3, element 5 to element 4, element 6 to element 5.

How do I remove a specific element from an array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.


unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array

array_splice($array, 0, 1);

http://php.net/manual/en/function.array-splice.php


You better use array_shift(). That will return the first element of the array, remove it from the array and re-index the array. All in one efficient method.


array_splice($array, array_search(array_value, $array), 1);