How to reindex an array?

PhpArrays

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

Use array_values:

$reindexed_array = array_values($old_array);

Solution 2 - Php

array_splice($old_array, 0, 0);

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

Solution 3 - Php

By using sort($array);

See PHP documentation here.

I'd recommend sort over array_values as it will not create a second array. With the following code you now have two arrays occupying space: $reindexed_array and $old_array. Unnecessary.

$reindexed_array = array_values($old_array);

Solution 4 - Php

From PHP7.4, you can reindex without a function call by unpacking the values into an array with the splat operator. Consider this a "repack".

Code: (Demo)

$array = array(
  0 => 'val',
  2 => 'val',
  3 => 'val',
  5 => 'val',
  7 => 'val'
);

$array = [...$array];

var_export($array);

Output:

array (
  0 => 'val',
  1 => 'val',
  2 => 'val',
  3 => 'val',
  4 => 'val',
)

Note: this technique will NOT work on associative keys (the splat operator chokes on these). Non-numeric demo

The breakage is reported as an inability to unpack string keys, but it would be more accurate to say that the keys must all be numeric. Integer as string demo and Float demo

Solution 5 - Php

array_splice($jam_array, 0, count($jam_array));

To sort an array with missing intermediate indices, with count the order is more secure. So 0 is the first index and count($jam_array) or sizeof($jam_array) return the decimal position of the array, namely, last index.

Solution 6 - Php

It's worth pointing out that the array in the question is a very special case, where all values are the same and keys are already in ascending order. If you have an array with different values and keys in any random order, and you want to sort it by key and reindex the keys, the existing answers will not do what you expect.

For example, if you have something like this:

[    7 => 'foo',    0 => 'bar',    2 => 42];

and you want to end up with this:

[    0 => 'bar',    1 => 42,    2 => 'foo',]

you can't just use array_values(), array_splice() or sort(), because you would end up with

[    0 => 'foo',    1 => 'bar',    2 => 42,]

Instead, you first need to sort the array based on the keys, using ksort(), and then reindex the keys using array_values():

$arr = [
    7 => 'foo',
    0 => 'bar',
    2 => 42
];

ksort($arr, SORT_NUMERIC);
$arr = array_values($arr);

Result:

[    0 => 'bar',    1 => 42,    2 => 'foo',]

Solution 7 - Php

The simple and easy way is:

$new_numerical_indexed_array = array_values($old_array);

supported by PHP 4, PHP 5, PHP 7, PHP 8 as you can see it https://www.php.net/manual/en/function.array-values.php"> here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstergoszView Question on Stackoverflow
Solution 1 - PhpEmil VikströmView Answer on Stackoverflow
Solution 2 - PhpalekveritovView Answer on Stackoverflow
Solution 3 - PhpRawkodeView Answer on Stackoverflow
Solution 4 - PhpmickmackusaView Answer on Stackoverflow
Solution 5 - PhpfvlgnnView Answer on Stackoverflow
Solution 6 - PhpMagnusView Answer on Stackoverflow
Solution 7 - PhpinfomasudView Answer on Stackoverflow