在PHP中更改多维数组的键

时间:2013-10-18 03:50:37

标签: php

这是我的第一个数组

Array
(
   [0] => Array
        (
            [0] => 1
            [1] => Elite
            [2] => Air-Con Bus
            [3] => Monday
        )

    [1] => Array
        (
            [0] => 4
            [1] => KBZ
            [2] => Airplane
            [3] => Wednesday
        )
    [2] => Array
        (
            [0] => 5
            [1] => Yoma
            [2] => Cruise
            [3] => Tuesday
        )
)

我想成为外部数组键的内部array[0]。像下面的数组:我可以吗?请建议我。

Array(
     [1] => Array
        (
            [0] => 1
            [1] => Elite
            [2] => Air-Con Bus
            [3] => Monday
        )

    [4] => Array
        (
            [0] => 4
            [1] => KBZ
            [2] => Airplane
            [3] => Wednesday
        )

    [5] => Array
        (            
            [0] => 5
            [1] => Yoma
            [2] => Cruise
            [3] => Tuesday
        )
)

2 个答案:

答案 0 :(得分:1)

$new_array = array();
foreach ($old_array as $el) {
    $new_array[$el[0]] = $el;
}

答案 1 :(得分:1)

单步:

foreach ($array as $a){
$new[$a[0]]=$a;
}