我需要从每个数组中取消设置[device_id]元素。 我尝试了递归。但我无法做到这一点。任何人都可以为此提供帮助。我试过这样的。
foreach($tree as $key) {
foreach($key as $value => $k){
if ($value == 'device_id'){
unset($k['device_id']);
}
}
}
我有嵌套的多维数组,如下所示:
Array
(
[4] => Array
(
[device_id] => 4
[device_parent_id] => 2
[device_ext] => 20122
[device_name] => section controller1
[username] => sectioncontroller1
[password] => 90714771f0abd448f208e1aff5b662fc
[device_macaddress] =>
[device_ip] =>
[device_desc] =>
[admin_id] => 2
[children] => Array
(
[5] => Array
(
[device_id] => 5
[device_parent_id] => 4
[device_ext] => 20199
[device_name] => Emergency1
[username] => Emergency1
[password] => bb75df6eb4fdbf5753640a34674ecce1
[device_macaddress] =>
[device_ip] =>
[device_desc] =>
[admin_id] => 2
[children] => Array
(
)
)
[6] => Array
(
[device_id] => 6
[device_parent_id] => 4
[device_ext] => 20111
[device_name] => HQ Phone
[username] => HQPhone
[password] => 2f89d97f24539dc50ddd8fc53667b194
[device_macaddress] =>
[device_ip] =>
[device_desc] =>
[admin_id] => 2
[children] => Array
(
)
)
[9] => Array
(
[device_id] => 9
[device_parent_id] => 4
[device_ext] => 20144
[device_name] => Other controller1
[username] => Othercontroller1
[password] => 861426b485e2afd62ad0d914f524f23c
[device_macaddress] =>
[device_ip] =>
[device_desc] =>
[admin_id] => 2
[children] => Array
(
[10] => Array
(
[device_id] => 10
[device_parent_id] => 9
[device_ext] => 30122
[device_name] => Way Station1
[username] => WayStation1
[password] => 085d397b0392d3a325216a716c757113
[device_macaddress] =>
[device_ip] =>
[device_desc] =>
[admin_id] => 2
[children] => Array
(
)
)
)
)
)
)
[21] => Array
(
[device_id] => 21
[device_parent_id] => 2
[device_ext] => 1123299
[device_name] => sectioncontroller99
[username] => sectioncontroller3
[password] => 73d25d443117f76c6fe6bd5a4679f458
[device_macaddress] => 124511251419
[device_ip] => 124.235.63.569
[device_desc] => test section9
[admin_id] => 0
[children] => Array
(
[22] => Array
(
[device_id] => 22
[device_parent_id] => 21
[device_ext] => 9999
[device_name] => emergancy
[username] => emergancy
[password] => c2bc24d3d41ad9ade0171c3d5d6f59c6
[device_macaddress] => 1245122
[device_ip] => 124.235.235
[device_desc] => sdfdf sdfsdf sdfsd
[admin_id] => 0
[children] => Array
(
)
)
)
)
);
答案 0 :(得分:1)
你需要一个递归函数。这应该有效:
function deleteDeviceId(&$tree) {
foreach ($tree as $k => &$item) {
unset($item['device_id']);
deleteDeviceId($item['children']);
}
}
deleteDeviceId($tree);
答案 1 :(得分:1)
问题是循环中的变量是独立变量,更改它们不会影响原始数组。
要解决这个问题,你可以:
$tree
中的完整路径直接取消设置变量:unset($tree[$outer_key][$inner_key]);
。你也需要在外循环中使用密钥。foreach
循环中使用了密钥和值,但这只是名字......