在多维数组中搜索值并在PHP中获取其路径 [英] search for a value in a multi dimensional array and get its path in PHP

查看:92
本文介绍了在多维数组中搜索值并在PHP中获取其路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组:

$array = array (
'1' => array(
  'title' => 'Level1',
  'nodes' => array(
   '11' => array('title' => 'sub1_company1'),
   '12' => array('title' => 'sub2_company1'),
   '13' => array(
     'title' => 'sub3_company1',
     'nodes' => array(
       '131' => array('title' => 'item1_sub3_company1'),
       '132' => array('title' => 'item2_sub3_company1'),
      ),
    ),
  ),
),

'2' => array(
  'title' => 'Level2',
  'nodes' => array(
   '21' => array('title' => 'sub1_company2'),
   '22' => array('title' => 'sub2_company2'),
  ),
),

'3' => array(
  'title' => 'Level3',
  'nodes' => array(
   '31' => array('title' => 'sub1_company3'),
   '32' => array(
     'title' => 'sub2_company3',
     'nodes' => array(
       '321' => array('title' => 'item1_sub2_company3'),
       '322' => array(
          'title' => 'item2_sub2_company3',
          'nodes' => array(
            '3221' => array('title' => 'item1_sub3_company3'),
          ),
        ),
      ),
    ),
  ),
),
'4' => array('title' => 'Level4'),);

我需要的是找到sub2_company1并获取像痕迹一样的标题。

What I need is to find sub2_company1 and get the titles like a breadcrumb.

Level1> sub2_company1

Level1 > sub2_company1

或者如果我搜索item1_sub3_company1我会得到

or if I search for item1_sub3_company1 I will get

Level1> sub3_company1> item1_sub3_company1

Level1 > sub3_company1 > item1_sub3_company1

到目前为止我做了什么但是没有工作:

What I did so far but isn't working:

    function breadcrumb($array, $needle) {
  $path = array();

  $array_iterator = new recursiveArrayIterator($array);
  $it = new recursiveIteratorIterator($array_iterator, RecursiveIteratorIterator::SELF_FIRST);

  foreach($it as $key => $value) 
  {
    echo "$key: $value <br>";
    if (!is_array($value)) {
      array_push($path, $value);
    }
    if ($value === $needle) {
      break;
    }
  }

  //$content = '<div id="breadcrumb">' . implode('&nbsp;&#62;&nbsp;', $path) . '</div>';

  return print_r($path, 1);
}

谢谢

推荐答案

你需要一个递归函数,而不是迭代函数。

You need a recursive function, not an iterative one.

function breadcrumb($tree, $needle, &$result = array()) {

    $result = array();

    if (is_array($tree)) {
        foreach ($tree as $node) {
            if ($node['title'] == $needle) {
                $result[] = $node['title'];
                echo '1-';
                return true;
            } else if (!empty($node['nodes'])) {
                if (breadcrumb($node['nodes'], $needle, $result)){
                echo '2-';
                $result[] = $node['title'];
                return true;
                }
            }
        }
    } else {
        if ($tree == $needle) {
            echo '3-';
            $result[] = $tree;
            return true;
        }
    }
    return false;
}

breadcrumb($array, 'item1_sub3_company3', $result);

print_r($result);

面包屑被反转但你可以使用array_shift而不是push,你会得到正确的方法。 ..

The breadcrumb is inverted but you could use array_shift instead of push and you would have it the right way...

这篇关于在多维数组中搜索值并在PHP中获取其路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆