Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reindex an array?

Tags:

arrays

php

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?

like image 316
stergosz Avatar asked Jun 27 '12 11:06

stergosz


People also ask

What does Array_splice () function do give an example?

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

What is array indexing explain with example?

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.


2 Answers

Use array_values:

$reindexed_array = array_values($old_array); 
like image 76
Emil Vikström Avatar answered Oct 11 '22 23:10

Emil Vikström


array_splice($old_array, 0, 0); 

It will not sort array and will not create a second array

like image 36
alekveritov Avatar answered Oct 11 '22 22:10

alekveritov