Changing array key value in PHP

后端 未结 2 1722
滥情空心
滥情空心 2021-01-24 13:08

I have an array like below,

[test] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
            [3] => 13
                


        
相关标签:
2条回答
  • 2021-01-24 13:46

    The array_values() function returns an array containing all the values of an array and also it reset all the keys. you can do it as

    $arr = array(0 => 1, 1 => 3, 2 => 5, 3 => 13, 4 => 32, 5 => 51);
    $count = 1;
    $tempArr = array();
    foreach ($arr as $key => $val) {
        $tempArr[$count * 2] = $val;
        $count++;
    }
    var_dump($tempArr);exit;
    

    Try this code at your side.

    0 讨论(0)
  • 2021-01-24 13:47
    $newArray = array_combine(
        range(2,count($originalArray)*2,2),
        array_values($originalArray)
    );
    
    0 讨论(0)
提交回复
热议问题