搜索具有相关值的数组路径

发布于 2024-10-02 01:32:06 字数 269 浏览 3 评论 0原文

我正在开发菜单系统并正在解决一些复杂的问题。菜单是从数组生成的。该数组包含在 Pastebin 中,因为它非常大。我想在数组中搜索并获取我正在搜索的值的分层路径,同时还获取您运行的父级旁边的值。正如我所说,它相当复杂。

在pastebin中是数组和我想要函数返回的结果:

-->pastebin<--

I尝试编写这个函数很多次,但总是卡在中间。

I'm working on a menu system and am working on some complex issues. The menu is generated from a array. This array is included in a pastebin because it's really big. I want to search in the array and get the hierarchical path of the value I'm searching while also getting the values next to the parents you run trough. As I said its quite complex.

In the pastebin is the array and the result I want to function to return:

-->pastebin<--

I tried writing this function quite a few times but always get stuck in the middle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2024-10-09 01:32:06

这是一个功能:

function get_item_recursive($id, $menu_array = array())
{
    foreach($menu_array as $menu_item)
    {
         if(isset($menu_item['id']) && $menu_item['id'] == $id)
         {
             $menu_item['subitems'] = array();
             return $menu_item;
         }
         else
         {
             if(isset($menu_item['subitems']) && !empty($menu_item['subitems']))
             {
                 $found = get_item_recursive($id, $menu_item['subitems']);
                 if($found)
                 {
                     return $menu_item;
                 }
             }
         }
    }
    return FALSE;
}

我还没有测试过它,但这就是想法。

Here is a function:

function get_item_recursive($id, $menu_array = array())
{
    foreach($menu_array as $menu_item)
    {
         if(isset($menu_item['id']) && $menu_item['id'] == $id)
         {
             $menu_item['subitems'] = array();
             return $menu_item;
         }
         else
         {
             if(isset($menu_item['subitems']) && !empty($menu_item['subitems']))
             {
                 $found = get_item_recursive($id, $menu_item['subitems']);
                 if($found)
                 {
                     return $menu_item;
                 }
             }
         }
    }
    return FALSE;
}

I have not tested it, but this is the idea.

花开浅夏 2024-10-09 01:32:06

您基本上是在寻找构建面包屑之类的东西的路径?您可以为此使用递归函数:

function findPath($haystack, $id, $parents = array()) {
   foreach ($haystack as $k => $v) {
      if ($v['id'] == $id) {
         return array_merge($parents, array($v));
      } else if (!empty($v['subitems'])) {
         unset($v['subitems']);
         $return = findPath(
            $haystack[$k]['subitems'],
            $id,
            array_merge($parents, array($v))
         );
         if ($return) return $return;
      }
   }
   return false;
}

像这样执行此函数:

findPath($haystack, 11);

将返回:

Array (
   [in-balans] => Array
      (
         [id] => 2
         [slug] => in-balans
         [title] => In balans
      )
   [arbodienstverlening] => Array
      (
         [id] => 10
         [slug] => arbodienstverlening
         [title] => Arbodienstverlening
      )
   [arbo] => Array
      (
         [id] => 11
         [slug] => arbo
         [title] => Arbo
         [subitems] =>
      )
)

You are basically looking for the path to build something like a breadcrumb? You can use a recursive function for that:

function findPath($haystack, $id, $parents = array()) {
   foreach ($haystack as $k => $v) {
      if ($v['id'] == $id) {
         return array_merge($parents, array($v));
      } else if (!empty($v['subitems'])) {
         unset($v['subitems']);
         $return = findPath(
            $haystack[$k]['subitems'],
            $id,
            array_merge($parents, array($v))
         );
         if ($return) return $return;
      }
   }
   return false;
}

Executing this function like this:

findPath($haystack, 11);

Would return:

Array (
   [in-balans] => Array
      (
         [id] => 2
         [slug] => in-balans
         [title] => In balans
      )
   [arbodienstverlening] => Array
      (
         [id] => 10
         [slug] => arbodienstverlening
         [title] => Arbodienstverlening
      )
   [arbo] => Array
      (
         [id] => 11
         [slug] => arbo
         [title] => Arbo
         [subitems] =>
      )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文