44

What is the simplest solution to increase by 1 all keys in an array?

BEFORE:

$arr[0] = 'a';
$arr[1] = 'b';
$arr[2] = 'c';

AFTER:

$arr[1] = 'a';
$arr[2] = 'b';
$arr[3] = 'c';
Stanislav
  • 903
  • 2
  • 9
  • 14

6 Answers6

53

You can use

$start_zero = array_values($array); /* Re-Indexing Array To Start From Zero */

And if you want to start it from index 1 use

$start_one = array_combine(range(1, count($array)), array_values($array));
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • 2
    Thanks. That might work. I was looking for "one line" solution. :-) – Stanislav Oct 03 '12 at 19:41
  • 2
    Note this will *re-index* they array. Keep this in mind if your indexes are not sequential as technically that could alter the index by more or less than 1. – Jason McCreary Oct 03 '12 at 19:45
  • if i have array with keys from 0 to 4 and replace range(1, to range(5. it doesnt work neither increasing array, any suggestions ? – fearis Nov 23 '15 at 10:29
  • Thanks Mandara & Mr. Alien. I've figured it out. If you want to add to array follow this steps : $countPrevArray = count($arrayPrev); $arrayToAdd = array_combine(range($countPrevArray, count($addArray)+$countPrevArray-1), array_values($addArray)); $arrayPrev = $arrayPrev + $arrayToAdd; – fearis Nov 23 '15 at 10:37
  • Why is `array_values()` needed in the second line? – AbraCadaver Sep 05 '16 at 23:38
  • Be aware that if the array is empty, it gives this error `array_combine(): Both parameters should have an equal number of elements` – T30 Aug 31 '17 at 11:13
  • Unfortunately, this does not answer the actual question. This reindexes the array starting by one. OP asked how to increase by 1 all keys in the array. If there are missing keys, this code will not work as the question was originally asked. – Erick Robertson Feb 15 '20 at 17:57
34

Well, there's one very simple way to do it:

$arr = array('a', 'b', 'c');
array_unshift($arr, null);
unset($arr[0]);
print_r($arr);
/* 
Array
(
    [1] => a
    [2] => b
    [3] => c
)
*/

Will work only for simple dense arrays, of course.

And this is most untrivial (yet both a one-liner AND working for both dense and sparse arrays) way:

$arr = array_flip(array_map(function($el){ return $el + 1; }, array_flip($arr)));
raina77ow
  • 103,633
  • 15
  • 192
  • 229
13

I'm not sure why you'd want to do this, but you should just be able to loop through:

$new_array = array();
foreach($arr as $key => $value){
   $new_array[$key+1] = $value;
}
$arr = $new_array;
Ben D
  • 14,321
  • 3
  • 45
  • 59
  • 6
    @Stan: What do you mean by that? Are you explicitly trying to overcomplicate a trivial solution? – Madara's Ghost Oct 03 '12 at 19:37
  • I just was looking for a "one line" solution. – Stanislav Oct 03 '12 at 19:44
  • 1
    +1 from me. I think this is the most complete solution, as it will work both for dense and sparse arrays. Of course, it's not as list-comprehensive as other solutions, but it should be the fastest one AND the most readable. Still, I wonder didn't we just miss another `homework` candidate here. ) – raina77ow Oct 03 '12 at 19:48
  • 1
    This will bring you into trouble in some situation. If you increase [1] by one, then you will overwrite [2]. In your example it will work as foreach works with a copy of the array, but in oop or referenced programming this will kill you – Sliq Jan 03 '13 at 10:44
  • @Panique - It shouldn't matter in any case because the original array `$arr` is never touched until it is completely overwritten by `$new_array` in the last line. This is why we use `$new_array` as an intermediate. Is there a specific scenario you'd be worried about? – Ben D Jan 03 '13 at 22:34
  • @BenD Uh, you are totally right, i just commented under the wrong answer – Sliq Jan 04 '13 at 11:42
  • This is better than the `array_flip()` based answer because it will work with values that are non-scalar (i.e. array / object / boolean / etc.) – oucil Aug 10 '20 at 20:00
5
$count = count($arr);
for($i=$count; $i>0; $i--){
    $arr[$i] = $arr[$i-1];
}
unset($arr[0]);
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • except that this will result in an array with 4 elements – Ben D Oct 03 '12 at 19:37
  • Won't work with sparse arrays (like `array('a', 'b', 5 => 'c')`). – raina77ow Oct 03 '12 at 19:46
  • 2
    That wasn't a spec in the OP's question. It won't work with associative arrays either. Should we mark everybody's answer wrong since nobody's checks if the key is a string before applying math? – AlienWebguy Oct 03 '12 at 20:27
4

I know this question is quite old, but I ran into a similar issue recently and came up with a nice one-liner to solve it for any type of array using an arbitrary integer as the starting key:

$array = array_combine(array_keys(array_fill($starting_index, count($array), 0)), array_values($array));

$starting_index is whatever value you want for the initial integer key, e.g. 3.

This can even be used with arrays holding complex objects, unlike the solution using array_flip and does not limit you to starting the index at 0 or 1 like some of the other solutions.

Brian
  • 336
  • 1
  • 9
0

I'm not sure if this qualifies as a one liner but it is a different way of doing it

$result = array_reduce(array_keys($arr),function($carry,$key) use($arr){
     $carry[$key+1] = $arr[$key]; 
     return $carry;
},[]);
arcanine
  • 1,933
  • 1
  • 15
  • 22