如何从multidimentional数组中删除特定的键

时间:2016-06-10 09:58:10

标签: php arrays

我想从数组下面删除[0]键。

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [PMSE] => Array
                        (
                            [id] => 1
                            [employee_name] => John Deep
                            [phone] => 12345
                            [created_on] => 2016-06-07 08:23:58
                        )

                    [Custom] => Array
                        (
                            [projectEmployeeCount] => 3
                        )

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [PMSE] => Array
                        (
                            [id] => 1
                            [employee_name] => Alex Hell
                            [phone] => 12345
                            [created_on] => 2016-06-07 08:23:58
                        )

                    [Custom] => Array
                        (
                            [projectEmployeeCount] => 3
                        )

                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [PMSE] => Array
                        (
                            [id] => 1
                            [employee_name] => Doe Ria
                            [phone] => 12345
                            [created_on] => 2016-06-07 08:23:58
                        )

                    [Custom] => Array
                        (
                            [projectEmployeeCount] => 2
                        )

                )

        )

)

欲望输出

Array
    (
        [0] => Array
            (
                [PMSE] => Array
                    (
                        [id] => 1
                        [employee_name] => John Deep
                        [phone] => 12345
                        [created_on] => 2016-06-07 08:23:58
                    )

                    [Custom] => Array
                    (
                        [projectEmployeeCount] => 3
                    )
            )

        [1] => Array
            (
                [PMSE] => Array
                    (
                        [id] => 2
                        [employee_name] => Alex Hell
                        [phone] => 12345
                        [created_on] => 2016-06-07 08:23:58
                    )

                    [Custom] => Array
                    (
                        [projectEmployeeCount] => 5
                    )
            )

        [2] => Array
            (
                [PMSE] => Array
                    (
                        [id] => 3
                        [employee_name] => Doe Grey
                        [phone] => 12333
                        [created_on] => 2016-06-07 08:23:58
                    )

                    [Custom] => Array
                    (
                        [projectEmployeeCount] => 4
                    )
            )


    )

4 个答案:

答案 0 :(得分:1)

循环并弹出内部数组。

foreach ($array as $val) {
  $newArray[] = array_pop($val);
}

var_dump($newArray);

答案 1 :(得分:1)

只需一行代码:

$array = array_map(function($value){ return $value[0]; },$array);

答案 2 :(得分:0)

你可以像这样做

如果你有例如:

$s = array(
    0=>array(
        0=>array(
            'pmse'=>array(
                'id'=>1,
                'employee'=>'john'),
            'custom'=>array(
                'projects'=>3)
            )
        ),
    1=>array(
        0=>array(
            'pmse'=>array(
                'id'=>2,
                'employee'=>'john'),
            'custom'=>array(
                'projects'=>4)
            )
        ),
    2=>array(
        0=>array(
            'pmse'=>array(
                'id'=>3,
                'employee'=>'john'),
            'custom'=>array(
                'projects'=>4)
            )
        ),
    );

然后你可以通过数组并将它们推送到另一个

$new_array = array();
foreach($s as $key1=>$arr1){
    foreach($arr1 as $key2=>$arr2){
         array_push($new_array, $arr2);
    }
}

$new_array现在是欲望数组

输出:

    [0] => array(2) {
        ["pmse"] => array(2) {
            ["id"] => int(1)
            ["employee"] => string(4)"john"
            ["custom"] => array(1) {
                ["projects"] => int(3)
            }
        }
    }[1] => array(2) {
        ["pmse"] => array(2) {
            ["id"] => int(2)
            ["employee"] => string(4) "john"
            ["custom"] => array(1) {
                ["projects"] => int(4)
            }
        }
    }[2] => array(2) {
        ["pmse"] => array(2) {
            ["id"] => int(3)
            ["employee"] => string(4) "john"
            ["custom"] => array(1) {
                ["projects"] => int(4)
            }
        }
    }
}

答案 3 :(得分:0)

写一下,

array_map('current', $array); 

OR

array_map('array_shift', $array);

如果您使用的是PHP5.5 +,那么以下代码也可以使用

array_column($array, 0);