How to Change the Array Key to Start from 1 Instead of 0

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

how to array key start from 1 instead of 0?

You can also go for :

$numbers = range(0, 14);

array_unshift($numbers,"");
unset($numbers[0]);

How can I reset array key and shift first key to 1 in php?

Please try following code.

function reindex ($arr, $start_index)
{
$arr = array_combine(range($start_index, count($arr) + ($start_index - 1)), array_values($arr));
return $arr;
}

Following is my test result.

$arr = array ( 
0 => 'apple',
1 => 'banana',
3 => 'orange',
4 => 'melon'
);

$arr = reindex ($arr, 1);

foreach( $arr as $key => $value) {
echo "Index: ".$key." Value: ".$value."\n";
}

And following is the output.

Index: 1 Value: apple
Index: 2 Value: banana
Index: 3 Value: orange
Index: 4 Value: melon

You can test this code on following online tester.

http://sandbox.onlinephpfunctions.com/

Starting array index with 1 instead of 0

If you don't want to alter value of key with +1 at the time of displaying, then maintain a counter variable $x which starts from 1 and keep on incrementing it in loop.

<?php
ini_set('display_errors', 1);
$x=1;
foreach ($export AS $exp)
{
$_SESSION['export'][$x] = array($exp->label, $exp->pos_X, $exp->pos_Y);
$x++;//added this line for incrementing value of $x
}

php explode and force array keys to start from 1 and not 0

$exploded = explode('.', 'a.string.to.explode');
$exploded = array_combine(range(1, count($exploded)), $exploded);
var_dump($exploded);

Done!

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

How I can index the array starting from 1 instead of zero?

Java arrays are always 0-based. You can't change that behavior. You can fill or use it from another index, but you can't change the base index.

It's defined in JLS §10.4, if you are interested in it.

A component of an array is accessed by an array access expression (§15.13) that consists of an expression whose value is an array reference followed by an indexing expression enclosed by [ and ], as in A[i].

All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1.

php array_map changes the first key to zero when it was originally one

After your $collections has been created, unshift the array with a garbage value and then delete it:

array_unshift($collections, null);
unset($collections[0]);

This will shift everything down by one, moving the first real element to index 1.



Related Topics



Leave a reply



Submit