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

how to make an array start at 1
php foreach key start at 1
php reindex array
array_unshift
php array
array shift php
php rotate array
array ( (0) => array)

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, ....

$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

change array index to start from 1 instead of 0 in php – Avinash Ware, I have one single part of my app where if my arrays could start at index 1 it ($​array as ($index + 1) => $val) { } or to somehow put a setting to make … to skip the first element, but you just want the key to be 1 instead of 0? If you pass a reseed value the DB will start the identity from that new value: DBCC CHECKIDENT (SyncSession, RESEED, 0); --next record should be 0 + increment You don't have to pass the a value though, if you don't IDENTITY(a,b) will be used instead: DBCC CHECKIDENT (SyncSession, RESEED); --next record should be the seed value 'a'

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
)

Array start at 1? - PHP, [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Example #2 'short syntax associative array' <?​php By telling the key to read the object as a string, it will let you set it. these are O(1) (without getting into what's going on in the interpreter), instead of O(n). "When index is omitted, an integer index is automatically generated, starting at 0. you could also use a for() loop but the concept is the same, you need to manually start a variable at 1 and increment it, or you need to add 1 to the current key. triexa August 30, 2014, 11:40am #6

Simply try this

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

array - Manual, All numerical array keys will be modified to start counting from zero while literal keys won't array_unshift() - Prepend one or more elements to the beginning of an array It sped up as the array shrank, most likely as it has to reindex a smaller data set. it returns a reference to the first element of the array instead of a copy. array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be changed.

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.

array_shift - Manual, $key . " Value: " . $value . "\n" ;. } // Set the index number from three. $​New_start_index = 3;. $raw_data = range( 0, $New_start_index -1 );. // Add data to the start  foreach ($array as $key => $val) { echo $key + 1, '<br>'; } Edit After the question has been clarified: You could use the array_values to reset the index starting at 0. Then you could use the algorithm above if you just want printed elements to start at 1.

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

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

How I can index the array starting from 1 instead of zero?, how to start array index from 1 in javascript array index to start from 1 instead of 0 in php Yes,It is possible to change the way array indexed. the the first value key should be 1 instead of zero i.e. By default in PHP the array key starts from 0. Here is a simple array_replace_keys function: /** * This function replaces the keys of an associate array by those supplied in the keys array * * @param $array target associative array in which the keys are intended to be replaced * @param $keys associate array where search key => replace by key, for replacing respective keys

Understand Arrays in PHP - Tuts+ Code, A common symptom of starting to access the values of your array at 1 instead of 0 is Table 6-1. A numeric array containing colors, starting at 0 Key Value 0 Black 1 Numeric arrays make it easier to loop through a set of data, since you need  Parameters. start_index. The first index of the returned array. If start_index is negative, the first index of the returned array will be start_index and the following indices will start from zero (see example).

Creating Arrays in PHP, Table 6-1. A numeric array containing colors, starting at zero Key Value 0 Black arrays are provided because they make it easier to loop through a set of data,  myNumbers is now an array with two arrays as its elements. To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers:

PHP Arrays, The key point here is that nullable types enable you to easily work with nullable As a result, to create an Integer array, a set of parentheses is added to the of Visual Basic, it is possible to specify that an array should start at 1 instead of 0. C++ Arrays. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store:

Comments
  • Is there any reason you can't just use a zero based array?
  • @Jacob, for instance, a for loop that uses % == 0 to define <tr> and <td> tags. An $i[0] trips the </tr>. Anyway, that's how I ended up at this question :)
  • Why are there so many upvotes on this question? What's wrong with 0-based index?
  • Thanks Michael Mctirnan,
  • FYI, In 2nd approach, $i= 1; foreach($stack1 as $value){ $stack2[$i] = $value; $i++; } will not perform same as foreach ($stack1 as $k => $val) { $stack2[$k+1] = $val; }.
  • This solution is easier and faster than the accepted solution.
  • I prefer this solution.
  • Your response ignores that we can not arbitrarily choose the index! Otherwise, the problem was not there!
  • Also this solution ignores, that sometimes array is generated dynamically and passed to variable, so we cannot access the first key without additional unnecessary code.
  • I prefer ur solution than others, since ricardo's solution is meant for static definition of array
  • I like your solution.
  • No, this is a wrong solution, array_filter will remove all empty values, not only the merged element. For example, if $array = array('a', 'b', '');, you will lose the 3rd element, which should be retained.
  • @Fishdrowned It is not meant to be a general purpose solution.
  • Excellent answer! This works well to have both index and value match and start from 1.
  • Or array_combine(range(1, 10), range(1, 10)) if all you need is numbers.