【问题标题】:Search in php multidimensional array在php多维数组中搜索
【发布时间】:2018-04-28 08:30:43
【问题描述】:

我的项目中有一个分层数组,如下所示:

$Array = array(
    array(
        'Id' => 1,
        'Title' => 'Some Text1',
        'Children' => array(
            array(
                'Id' => 11,
                'Title' => 'Some Text11',
                'Children' => array(
                    array(
                        'Id' => 111,
                        'Title' => 'Some Text111',
                    ),
                    array(
                        'Id' => 112,
                        'Title' => 'Some Text112',
                        'Children' => array(
                            array(
                                'Id' => 1121,
                                'Title' => 'Some Text1121',
                            )
                        )
                    )
                )
            ),
            array(
                'Id' => 12,
                'Title' => 'Some Text12',
                'Children' => array(
                    array(
                        'Id' => 121,
                        'Title' => 'Some Text121',
                    )
                )
            )
        )
    ),
    array(
        'Id' => 2,
        'Title' => 'Some Text2',
    )
);

我想在这个数组的'Title'索引中搜索我的字符串(例如'Some Text1121')并返回它的路径,例如搜索后'Some Text1121'我想返回这个结果:

"1 -> 11 -> 112 -> 1121"

或者当我搜索“Some”字符串时,返回数组中的所有路径。 请帮助我,谢谢。

【问题讨论】:

  • 您想要完全匹配还是相似列表?如果您尝试了任何方法,请分享?
  • 你需要递归函数来做到这一点。
  • 这不是一个简单的内置函数就能解决的任务。将涉及自定义递归函数。但你要从某个地方开始..

标签: php arrays


【解决方案1】:

我很快就给你写了一些东西。它并不完美,但你明白了:

<?php

function searchRec($haystack, $needle, $pathId = Array(), $pathIndex = Array()) {
    foreach($haystack as $index => $item) {
        // add the current path to pathId-array
        $pathId[] = $item['Id'];
        // add the current index to pathIndex-array
        $pathIndex[] = $index;
        // check if we have a match
        if($item['Title'] == $needle) {
            // return the match
            $returnObject = new stdClass();
            // the current item where we have the match
            $returnObject->match = $item;   
            // path of Id's (1, 11, 112, 1121)
            $returnObject->pathId = $pathId; 
            // path of indexes (0,0,1,..) - you might need this to access the item directly
            $returnObject->pathIndex = $pathIndex; 
            return $returnObject;
        }

        if(isset($item['Children']) && count($item['Children']>0)) {
            // if this item has children, we call the same function (recursively) 
            // again to search inside those children:
            $result = searchRec($item['Children'], $needle, $pathId, $pathIndex);
            if($result) {
                // if that search was successful, return the match-object
                return $result;
            }
        }
    }
    return false;
}

// useage:
$result = searchRec($Array, "Some Text11");
var_dump($result);

// use 
echo implode(" -> ", $result->pathId);
// to get your desired 1 -> 11 -> 112

EDIT:重写以使函数实际返回一些东西。它现在返回一个带有匹配项的对象、Id 的路径和(数组)索引的路径。

【讨论】:

  • 谢谢我的朋友。
  • 我觉得这个很好,
猜你喜欢
  • 2016-09-19
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 2011-05-20
相关资源
最近更新 更多