我有一个如下所示的数组,我想要实现的基本上是删除像@attributes和AddressSop这样的键,并将它们全部放到一个级别,这样就删除了父键和数组,但保留了键值对:
[Addresses] => Array
(
[0] => Array
(
[AddrCountry] => US
[AddrLine1] => Test Street
[AddrLine2] => Test
[AddrLine3] => Test
[AddrName] => Mr John Doe
[AddrEmail] => test@test.com
[AddrMasterPriceBook] =>
[AddrMobile] => 123132142242
)
[1] => Array
(
...
)
)
这是原始数组的外观。任何帮助都会很棒,谢谢。
[Addresses] => Array
(
[0] => Array
(
[@attributes] => Array
(
[AddrCountry] => US
[AddrLine1] => Test Street
[AddrLine2] => Test
[AddrLine3] => Test
[AddrName] => Mr John Doe
)
[AddressSOP] => Array
(
[@attributes] => Array
(
[AddrEmail] => test@test.com
[AddrMasterPriceBook] =>
[AddrMobile] => 123132142242
)
)
)
[1] => Array
(
[@attributes] => Array
(
[AddrCountry] => US
[AddrLine1] => Test Street
[AddrLine2] => Test
[AddrLine3] => Test
[AddrName] => Mr John Doe
)
[AddressSOP] => Array
(
[@attributes] => Array
(
[AddrEmail] => test@test.com
[AddrMasterPriceBook] =>
[AddrMobile] => 123132142242
)
)
)
)
答案 0 :(得分:0)
试试这个:
foreach ($addresses as $key => $address) {
$addresses[$key] = array_merge($address['@attributes'], $address['AddressSOP']);
}
如果您希望它是递归的,您可以这样做:
$filtered = [];
foreach ($addresses as $addressKey => $address) {
array_walk_recursive($address, function($val, $key) use(&$filtered, $addressKey) {
$filtered[$addressKey][$key] = $val;
});
}
print_r($filtered);