Home » Php » php – how to change the array key to start from 1 instead of 0

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have values in some array I want to re index the whole array such that the the first value key should be 1 instead of zero i.e.

By default in PHP the array key starts from 0. i.e. 0 => a, 1=> b, I want to reindex the whole array to start from key = 1 i.e 1=> a, 2=> b, ....

How to&Answers:
$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

Answer:

Here is my suggestion:

<?php
$alphabet = array(1 => 'a', 'b', 'c', 'd');
echo '<pre>';
print_r($alphabet);
echo '</pre>';
?>

The above example will output:

Array
(
    [1] => a
    [2] => b
    [3] => c
    [4] => d
)

Answer:

Simply try this

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

Answer:

Ricardo Miguel’s solution works best when you’re defining your array and want the first key to be 1. But if your array is already defined or gets put together elsewhere (different function or a loop) you can alter it like this:

$array = array('a', 'b', 'c'); // defined elsewhere

$array = array_filter(array_merge(array(0), $array));

array_merge will put an array containing 1 empty element and the other array together, re-indexes it, array_filter will then remove the empty array elements ($array[0]), making it start at 1.

Answer:

from the manual
http://php.net/manual/en/function.array.php

$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);

Answer:

$array = array('a', 'b', 'c', 'd');
$array = array_combine(range(1, count($array)), array_values($array));
print_r($array);

the result:

Array
(
    [1] => a
    [2] => b
    [3] => c
    [4] => d
)

Answer:

If you are using a range, try this code:

$data = array_slice(range(0,12), 1, null, true);

// Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 )

Answer:

I see this answer is very old, but my solution for this is by adding a +1 to your index. I’ll do that because I think this is much faster and easy to read. When you print this it will start from 1 because 0+1 =1, then 2 etc.

  foreach($items as $index => $item){
 echo $index+1 . $item
}

Answer:

try this

<?php
$stack = array('a', 'b', 'c', 'd');
$i= 1;
foreach($stack as $value){
    $stack2[$i] = $value;
    $i++;
}
$stack = stack2;
?>