Re-Index Numeric Array Keys

Re-index numeric array keys

$your_new_array = array_values($your_old_array);

How do you reindex an array in PHP but with indexes starting from 1?

If you want to re-index starting to zero, simply do the following:

$iZero = array_values($arr);

If you need it to start at one, then use the following:

$iOne = array_combine(range(1, count($arr)), array_values($arr));

Here are the manual pages for the functions used:

  • array_values()
  • array_combine()
  • range()

Reset the numeric keys of an array

Use array_merge to reindex you array.

The code:

<?php
$array = array(
1 => 'one',
2 => 'two',
3 => 'three',
'row' => 'four',
'newRow' => 'five',
);
$reindexed_array = array_merge($array);
var_dump($reindexed_array);

The result:

array(5) {
[0]=> string(3) "one"
[1]=> string(3) "two"
[2]=> string(5) "three"
["row"]=> string(4) "four"
["newRow"]=> string(4) "five"
}

A working example you can find here: https://3v4l.org/Om72e. More information about array_merge: http://php.net/manual/en/function.array-merge.php

PHP reindex array?

Use array_values.

$myarray = array_values($myarray);

How to reindex an array?

Use array_values:

$reindexed_array = array_values($old_array);

Splatpacking versus array_values() to re-index an array with numeric keys

When re-indexing a 450,000 element array which has its first element unset...

array_values() is consistently twice as fast as splatpacking.

$array = range(0, 450000);
unset($array[0]);

Benchmark script

Sample output:

Duration of array_values: 15.328378677368
Duration of splat-pack: 29.623107910156

In terms of performance, you should always use array_values(). This is one case when a function-calling technique is more efficient than a non-function-calling technique.


I suppose the only scenario where the splatpacking technique wins is if you are a CodeGolfer -- 13 characters versus 5.

Remove key from mixed array and reindex

You can filter the array by their key. Live demo.

unset($array['status']);
$number_keys = array_values(array_filter($array, function($k){return is_int($k) && $k != 1;}, ARRAY_FILTER_USE_KEY));
$nonnumber_keys = array_filter($array, function($k){return is_string($k);}, ARRAY_FILTER_USE_KEY);
$result = array_merge($number_keys, $nonnumber_keys);

php re-index numeric keys in associative array with combined numeric and string keys

If your array isn't very large, you could build another array:

$output = [];

foreach ($original as $key => $item):
if(is_string($key)) $output[$key] = $item;
else $output[] = $item;
endforeach;

$output is what you want.

Update:

You could also unshift (insert) a dummy element at the start of the array, then shift (remove) it. This will re-key the numerical keys and leave the string keys alone:

array_unshift($original,'dummy');
array_shift($original);

thanks to @brennonbrimhall for the inspiration.

Re-index numeric nested arrays keys

You can use array_values + recursively calling custom function:

function arrayValuesRecursive($array) {
$array = array_values($array);
$countValues = count($array);
for ($i = 0; $i < $countValues; $i++ ) {
$subElement = $array[$i];
if (is_array($subElement)) {
$array[$i] = arrayValuesRecursive($subElement);
}
}
return $array;
}

$restructuredArray = arrayValuesRecursive($array);


Related Topics



Leave a reply



Submit