【问题标题】:Find specific element from multidimensional array in PHP从 PHP 中的多维数组中查找特定元素
【发布时间】:2019-03-19 17:53:53
【问题描述】:

我有一个用于构建导航菜单的多维数组。它可以由任意数量的子菜单(或子菜单)组成。菜单工作得很好。当有人点击菜单链接时,会打开 ID 为“menuid”的产品类别。但是,我还需要知道当前 menuid 的所有孩子的 menuid(但不是孙子等等)。

这是数组的一个例子:

    Array
(
   [0] => Array
      (
         [menutype] => url
         [menuid] => 46
      )
   [1] => Array
      (
         [menutype] => product_category
         [menuid] => 55
         [children] => Array
            (
               [0] => Array
                  (
                     [menutype] => product_category
                     [menuid] => 69
                     [children] => Array
                        (
                           [0] => Array
                              (
                                 [menutype] => product_category
                                 [menuid] => 211
                              )
                           [1] => Array
                              (
                                 [menutype] => product_category
                                 [menuid] => 57
                              )
                           [2] => Array
                              (
                                 [menutype] => product_category
                                 [menuid] => 166
                              )
                        )
                  )
               [1] => Array
                  (
                     [menutype] => product_category
                     [menuid] => 57
                  )
               [2] => Array
                  (
                     [menutype] => product_category
                     [menuid] => 94
                  )
            )
      )
   [2] => Array
      (
         [menutype] => posts_category
         [menuid] => 45
      )
)

例如,我想知道如何为 menuid 为 69 的元素获取 children 中元素的 menuid 值。(应该返回一个 211 的数组, 57 和 166)。

【问题讨论】:

  • 你有什么代码可以让菜单正常工作
  • 最简单的方法是构建一个递归函数基本上是一个你将传入一个数组的函数,然后如果有一个子数组你'将从函数内部调用函数。网上有很多例子。
  • AbraCadaver 菜单的代码过于广泛,无法在此处发布,尤其是因为它不相关。 :) 我帖子里的数组已经编辑过了,原来数组的每个元素都有很多数据(除了menutype、menuid和children)。

标签: php arrays multidimensional-array associative-array


【解决方案1】:

您可以使用如下递归函数来完成此操作:

function getChildIds($menuItems, $parentId) {
    foreach ($menuItems as $menuItem) {
        if (isset($menuItem['children'])) {
            $result = getChildIds($menuItem['children'], $parentId);
            if ($result !== false) {
                return $result;
            }
        }
        if ($menuItem['menuid'] == $parentId) {
            $result = [];
            if (isset($menuItem['children'])) {
                foreach ($menuItem['children'] as $childItem) {
                    $result[] = $childItem['menuid'];
                }
            }
            return $result;
        }
    }
    return false;
}

请注意,如果找到 menuid 但没有子代,则返回空数组,如果未找到 id,则返回 false。

【讨论】:

  • 哇,真快。开箱即用(尽管必须纠正“孩子”的拼写错误)。 :-)
【解决方案2】:

你也可以像这样更有效地使用递归函数:

$menu = [
    [
        'menutype' => 'url',
        'menuid'   => 46,
    ],
    [
        'menutype' => 'product_category',
        'menuid'   => 55,
        'children' => [
            [
                'menutype' => 'product_category',
                'menuid'   => 69,
                'children' => [
                    [
                        'menutype' => 'product_category',
                        'menuid'   => 211
                    ],
                    [
                        'menutype' => 'product_category',
                        'menuid'   => 57
                    ],
                    [
                        'menutype' => 'product_category',
                        'menuid'   => 166
                    ]
                ]
            ],
            [
                'menutype' => 'product_category',
                'menuid'   => 57
            ],
            [
                'menutype' => 'product_category',
                'menuid'   => 94
            ]
        ]
    ],
    [
        'menutype' => 'posts_category',
        'menuid'   => 45
    ]
];

function getMenu(array $menu, $menuId, $children = true)
{
    foreach ($menu as $menuItem) {
        if (array_key_exists('menuid', $menuItem) && $menuItem['menuid'] == $menuId) {
            if ($children === true && array_key_exists('children', $menuItem)){
                return $menuItem['children'];
            }

            return $menuItem;
        }

        if (array_key_exists('children', $menuItem)) {
            return getMenu($menuItem['children'], $menuId, $children);
        }
    }
}

getMenu($menu, 69);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2012-11-03
    相关资源
    最近更新 更多