我有这个数组
$array = Array
(
Array
(
"id" => 1,
"name" => "abc"
),
Array
(
"id" => 2,
"name" => 6
),
Array
(
"id" => 3,
"name" => "level 1",
"sub_items" => Array
(
Array
(
"id" => 5,
"name" => "level 2",
"sub_items" => Array
(
Array
(
"id" => 7,
"name" => "delete this!",
"sub_items" => Array
(
Array
(
"id" => 9,
"name" => "I will die too"
),
Array
(
"id" => 10,
"name" => "I will die too"
)
)
)
)
),
Array
(
"id" => 6,
"name" => "deleteeeeee me"
),
Array
(
"id" => 8,
"name" => "leave alone"
)
)
),
Array
(
"id" => 4,
"name" => "hello"
)
);
我想要的是找到具有特定键和值的给定节点,并将其与所有子节点(如果存在)一起删除。因此,如果要删除ID = 6
,那么我的数组就像这样
Array
(
[0] => Array
(
[id] => 1
[name] => abc
)
[1] => Array
(
[id] => 2
[name] => 6
)
[2] => Array
(
[id] => 3
[name] => level 1
[sub_items] => Array
(
[0] => Array
(
[id] => 5
[name] => level 2
[sub_items] => Array
(
[0] => Array
(
[id] => 7
[name] => delete this!
[sub_items] => Array
(
[0] => Array
(
[id] => 9
[name] => I will die too
)
[1] => Array
(
[id] => 10
[name] => I will die too
)
)
)
)
)
[1] => Array
(
[id] => 8
[name] => leave alone
)
)
)
[3] => Array
(
[id] => 4
[name] => hello
)
)
或者,如果我想删除ID = 7
,我会得到:
Array
(
[0] => Array
(
[id] => 1
[name] => abc
)
[1] => Array
(
[id] => 2
[name] => 6
)
[2] => Array
(
[id] => 3
[name] => level 1
[sub_items] => Array
(
[0] => Array
(
[id] => 5
[name] => level 2
[sub_items] => Array
(
)
)
[1] => Array
(
[id] => 6
[name] => deleteeeeee me
)
[2] => Array
(
[id] => 8
[name] => leave alone
)
)
)
[3] => Array
(
[id] => 4
[name] => hello
)
)
我发现的一些功能只是删除了密钥,而不是整个结构。例如:
Array
(
//here was "id" => 6
"name" => "deleteeeeee me"
),
我应该提到数组可以是N级深度,并且可以包含任何类型的键值对。因此ID = X / NAME = Y
我可以VALUE = X / AMMOUNT = Y / LOCATION = Z
这是codepad与我迄今为止所做的工作。
答案 0 :(得分:1)
最好的方法是使用递归函数(因为你的数组是多维的)。 由于您的密钥未预先定义,因此密钥名称应该是此功能的参数。 我假设sub_items被预先定义为保存子元素的数组的关键。
我会使用类似的东西($ arr是当前级别的数组,注意& in参数强制通过引用传递数组):
//function definition for use later
function remove_element_by_key ($keyName, $keyValue, & $arr = array()) {
//$arr is our current array, $ind is numerical index, $data is an array to compare keys
foreach ($arr as $ind => & $data) {
//check if current element is what we are looking for, ie. has 'id' => 6,
//and remove it if it is
if (array_key_exists($keyName, $data) && $data[$keyName] == $keyValue) {
unset($arr[$ind]);
}
//if not - search it's subelements
elseif (array_key_exists('sub_items', $data)) {
remove_element_by_key($keyName, $keyValue, $data['sub_items']);
}
}
}
//and after definition just use this:
remove_element_by_key('id', 6, $yourWholeArray);
我还没有测试过,但这肯定是要走的路。