我有一个数组数组,
我在下面发布了基本结构,递归可以是任何深度。
无论如何,我想要做的是,遍历数组中的每个项目,如果它的['item']->data('id')
值等于$id
然后删除树的那个部分,
但我不确定最有效的方法是什么?
我认为这可能有效,但我错过了什么吗?
function removeKey($key, &$categories){
foreach($categories as $k => $category){
if($category['item']->data('id') == $key){
unset($categories[$k]);
return;
}
if(!empty($category['children']))
removeKey($key, $category['children']);
}
}
removeKey($id, $categories);
array(
array(
'item' => "category Object", //use ->data('id') to get the id!
'children' => array(
array(
'item' => "category Object",
'children' => array(
array(
'item' => "category Object",
'children' => array()
)
)
),
array(
'item' => "category Object",
'children' => array(
array(
'item' => "category Object",
'children' => array()
)
)
)
)
),
array(
'item' => "category Object",
'children' => array(
array(
'item' => "category Object",
'children' => array(
array(
'item' => "category Object",
'children' => array()
)
)
),
array(
'item' => "category Object",
'children' => array(
array(
'item' => "category Object",
'children' => array()
)
)
)
)
)
);