My array looks like this:
array( 0 => 'val', 2 => 'val', 3 => 'val', 5 => 'val', 7 => 'val' );
How can I reset the keys so it will go like 0, 1, 2, 3, 4
?
The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter (See Example 2).
PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0. PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.
Use array_values:
$reindexed_array = array_values($old_array);
array_splice($old_array, 0, 0);
It will not sort array and will not create a second array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With