删除多维数组的第一个键

时间:2015-10-15 20:55:58

标签: php arrays multidimensional-array

我的数据库中有一个数组,如下所示:

Array
(
    [0] => Array
        (
            [A] => Array
                (
                    [0] => A
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

        )

    [1] => Array
        (
            [C] => Array
                (
                    [0] => C
                    [1] => 6.9167
                    [2] => 0
                    [3] => 0
                )

        )

    [2] => Array
        (
            [D] => Array
                (
                    [0] => D
                    [1] => 6.9167
                    [2] => 0
                    [3] => 0
                )

        )

)

如何从此多维数组中删除第一个键( [0] => A, [0] => C, [0] => D )

这是我想要的数组输出:

Array
    (
        [0] => Array
            (
                [A] => Array
                    (
                        [0] => 0
                        [1] => 0
                        [2] => 0
                    )

            )

        [1] => Array
            (
                [C] => Array
                    (
                        [0] => 6.9167
                        [1] => 0
                        [2] => 0
                    )

            )

        [2] => Array
            (
                [D] => Array
                    (
                        [0] => 6.9167
                        [1] => 0
                        [2] => 0
                    )

            )

    )

3 个答案:

答案 0 :(得分:1)

最简单(我没有时间进行非循环播放):

foreach($array as $k1 => $inner) {
   foreach($inner as $k2 => $value) {
       unset($array[$k1][$k2][0]);
   }
}

答案 1 :(得分:0)

这很糟糕但应该适合你。它使用2级键作为要在下一级删除的元素的值

elif any(x in ["1", "2"] for x in choice) and not key:

答案 2 :(得分:0)

使用unset

$array = array(
    array(
        'A' => array(
            'A', 0, 0, 0
        )
    ),
    array(
        'C' => array(
            'C', 0, 0, 0
        )
    )
);


foreach ($array as $key => $value) {
    $erase = key($value);
    unset($array[$key][$erase][0]);
    // this resets the ordinal sequence from 1,2,3 to 0,1,2
    $a[$key][$erase] = array_values($array[$key][$erase]);
}