How to increase by 1 all keys in an array?

前端 未结 6 1624
北恋
北恋 2020-12-05 07:04

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

BEFORE:

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


        
6条回答
  •  庸人自扰
    2020-12-05 07:18

    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;
    

提交回复
热议问题