1

Let's say that I have the following array :

Array ( [0] => Jonny Cash [1] => Robert Smith )

How can I increment the array key in order to get something like this:

Array ( [1] => Jonny Cash [2] => Robert Smith )

Thanks in advance!

The Alpha
  • 143,660
  • 29
  • 287
  • 307
dimlo
  • 25
  • 4

3 Answers3

5

Push a null on to the front of the array and then delete it:

array_unshift($list, null);
unset($list[0]);
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
3

If you just want to make an existing array have a 1-based index, just add a fake entry at 0 and remove it:

// Assuming $array = Array ( [0] => Jonny Cash [1] => Robert Smith );

array_unshift($array, "fake");
unset($array[0]);
sbeliv01
  • 11,550
  • 2
  • 23
  • 24
0
$array = array(1 => 'Jonny Cash', 2 => 'Robert Smith');
print_r($array);

Output:

Array ( [1] => Jonny Cash [2] => Robert Smith )

Blazer
  • 14,259
  • 3
  • 30
  • 53