简体   繁体   中英

how to remove specific key from multidimentional array

I want to remove [0] key from below array.

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
                        )

                )

        )

)

Desire Output

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
                    )
            )


    )

Loop it and pop the inside array.

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

var_dump($newArray);

仅一行代码:

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

You can do this simply like that

if You have for example:

$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)
            )
        ),
    );

Then you can go through arrays and push them to another one

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

the $new_array is now the desire Array

The Output:

    [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)
            }
        }
    }
}

Just write,

array_map('current', $array); 

OR

array_map('array_shift', $array);

If you are using PHP5.5+ then below code also work

array_column($array, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM