删除多维数组中的父键

时间:2017-09-25 16:37:20

标签: php arrays multidimensional-array

我想从数组中删除键[Properties],如下所示

我拥有它:

Array(
 [Values] => 1
 [List] => Array(
     [Product] => Array( 
         [Details] => Array( 
            [Properties] => Array( 
                [Id] => 1
            )
         )
     )
  )
)

我想删除[properties]:

Array(
  [Values] => 1
  [List] => Array(
    [Product] => Array( 
        [Details] => Array( 
            [ID] => 1
        )
     )
  )
)

我试过了:

$result = array_map(function($sub) { return $sub['Properties']; }, $array);

$array= array_column($array, 'Properties');

不幸的是它不起作用。 我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

直向前:

$arr = [
    "Values" => 1,
    "List" => ["Product" => [ "Details" => ["Properties" => ["Id" => 1] ] ] ]
];

$arr['List']['Product']['Details']['Id'] = $arr['List']['Product']['Details']['Properties']['Id'];
unset($arr['List']['Product']['Details']['Properties']);

print_r($arr);

输出:

Array
(
    [Values] => 1
    [List] => Array
        (
            [Product] => Array
                (
                    [Details] => Array
                        (
                            [Id] => 1
                        )
                )
        )
)