我有一个下面的PHP数组,您可以在其中查看基于“ parent_id”和“ parent”元素的类别/子类别结构。 现在的要求是,基于“ menu_display_special = 1”条件,我要删除该键以及以该键的id为parent_id的键。
注意:如果条件“ menu_display_special = 1”匹配,则应从数组中删除类别和子类别(如果有)中的键。
结果应仅返回一个数组元素。即id = 2378
Array
(
[35] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2375
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2376
[parent] =>
)
[36] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2377
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
)
)
[parent_id] => 2376
[parent] => 1
)
[37] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2379
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2377
[parent] =>
)
[38] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2380
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2377
[parent] =>
)
[39] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2381
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[menu_display_special] => 1
)
)
[parent_id] => 2377
[parent] =>
)
[40] => Joomla\CMS\Menu\MenuItem Object
(
[id] => 2378
[params:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
)
)
[parent_id] => 2376
[parent] =>
)
)
var_export之后的代码:
array (
35 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2375',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2376',
'parent' => false,
)),
36 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2377',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2376',
'parent' => true,
)),
37 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2379',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2377',
'parent' => false,
)),
38 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2380',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
'menu_display_special' => '1',
)),
)),
'parent_id' => '2377',
'parent' => false,
)),
39 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2381',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
)),
)),
'parent_id' => '2377',
'parent' => false,
)),
40 =>
Joomla\CMS\Menu\MenuItem::__set_state(array(
'id' => '2378',
'params' =>
Joomla\Registry\Registry::__set_state(array(
'data' =>
stdClass::__set_state(array(
)),
)),
'parent_id' => '2376',
'parent' => false,
)),
)
编码尝试:但是它返回2个数组元素,即2381和2378。它应该只给我2378,因为2381是2377的子元素(其menu_display_special值为1)
$listArray = array_filter($list,
function($v) use($list) {
return $v->params->get('menu_display_special') != '1' && ($v->parent == TRUE || $list[$v->parent_id]['params']['menu_display_special'] != '1');
}
);
答案 0 :(得分:0)
我不确定您的设计要求是否允许我在递归函数中合并条件,因此我将它们分开以尝试最终准确。
没有返回值,在$array
之前加一个&
,以使其修改最初传递的输入数组。
代码(Demo)
function recurse(&$array, $pid = null) {
foreach ($array as $id => $item) {
if (isset($item->params->data->menu_display_special) && $item->params->data->menu_display_special == 1) {
unset($array[$id]);
recurse($array, $item->id);
} elseif ($item->parent_id === $pid) {
unset($array[$id]);
}
}
}
$list = [
35 => (object)[
'id' => '2375',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2376',
'parent' => false
],
36 => (object)[
'id' => '2377',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2376',
'parent' => false
],
37 => (object)[
'id' => '2379',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2377',
'parent' => false
],
38 => (object)[
'id' => '2380',
'params' => (object)[
'data' => (object)[
'menu_display_special' => '1'
]
],
'parent_id' => '2377',
'parent' => false
],
39 => (object)[
'id' => '2381',
'params' => (object)[
'data' => (object)[]
],
'parent_id' => '2377',
'parent' => false
],
40 => (object)[
'id' => '2378',
'params' => (object)[
'data' => (object)[]
],
'parent_id' => '2376',
'parent' => false
],
];
recurse($list);
var_export($list);
输出:
array (
40 =>
(object) array(
'id' => '2378',
'params' =>
(object) array(
'data' =>
(object) array(
),
),
'parent_id' => '2376',
'parent' => false,
),
)