How to Reindex an Array in PHP But With Indexes Starting from 1

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

PHP reindex array?

Use array_values.

$myarray = array_values($myarray);

how to change the array key to start from 1 instead of 0

$alphabet = array("a", "b", "c");
array_unshift($alphabet, "phoney");
unset($alphabet[0]);

Edit: I decided to benchmark this solution vs. others posed in this topic. Here's the very simple code I used:

$start = microtime(1);
for ($a = 0; $a < 1000; ++$a) {
$alphabet = array("a", "b", "c");
array_unshift($alphabet, "phoney");
unset($alphabet[0]);
}
echo (microtime(1) - $start) . "\n";


$start = microtime(1);
for ($a = 0; $a < 1000; ++$a) {
$stack = array('a', 'b', 'c');
$i= 1;
$stack2 = array();
foreach($stack as $value){
$stack2[$i] = $value;
$i++;
}
$stack = $stack2;
}
echo (microtime(1) - $start) . "\n";


$start = microtime(1);
for ($a = 0; $a < 1000; ++$a) {
$array = array('a','b','c');

$array = array_combine(
array_map(function($a){
return $a + 1;
}, array_keys($array)),
array_values($array)
);
}
echo (microtime(1) - $start) . "\n";

And the output:

0.0018711090087891
0.0021598339080811
0.0075368881225586

Reindex an array from a specific number

Probably not better, but here's another way to do this:

$customIndex = 5;
$output = [];
// example input array
$input = [1,2,3,4,5];

$indexes = range($customIndex, $customIndex + count($input) - 1);
$output = array_combine($indexes, $input);

var_dump($output);

// prints out:
array(5) {
[5]=>
int(1)
[6]=>
int(2)
[7]=>
int(3)
[8]=>
int(4)
[9]=>
int(5)
}

How to re-index arrays?

Just try with array_values:

$newArray = array_values($oldArray);

How to reindex an array?

Use array_values:

$reindexed_array = array_values($old_array);

Re-index numeric array keys

$your_new_array = array_values($your_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.

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

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


Related Topics



Leave a reply



Submit