簡體   English   中英

在php多維數組中搜索

[英]Search in php multidimensional array

我的項目中有一個分層數組,如下所示:

$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

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

編輯:重寫以使函數實際返回一些東西。 它現在返回一個帶有匹配項的對象、Id 的路徑和(數組-)索引的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM