从多维数组中获取特定值

时间:2015-08-19 04:10:20

标签: php arrays multidimensional-array

我有大的多维数组。我必须从中找到特定的子阵列 我尝试使用一个递归函数,但它实际上没有返回值。 任何人都可以给我另一个解决方案 这是数组的预览。

Array
(
    [0] => Array
        (
            [expanded] => 1
            [key] => _1
            [title] => New
        )

    [1] => Array
        (
            [key] => _2
            [title] => Home
        )

    [2] => Array
        (
            [expanded] => 1
            [key] => _3
            [title] => Care
            [children] => Array
                (
                    [0] => Array
                        (
                            [expanded] => 1
                            [key] => _4
                            [title] => face
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [key] => _5
                                            [title] => new
                                        )

                                    [1] => Array
                                        (
                                            [key] => _6
                                            [title] => <strong>face timeline</strong>
                                            [data] => Array
                                                (
                                                    [url] => http://localhost/patient/face-timeline/

                                                    [type] => content
                                                    [cid] => 2291
                                                    [timeline] => 0
                                                )

                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [key] => _2278
                                                            [title] => Post Op Visit
                                                        )

                                                    [1] => Array
                                                        (
                                                            [key] => _2277
                                                            [title] => Surgery
                                                        )

                                                    [2] => Array
                                                        (
                                                            [key] => _2276
                                                            [title] => Pre-Op
                                                        )

                                                    [3] => Array
                                                        (
                                                            [key] => _2275
                                                            [title] => Consultation
                                                        )

                                                    [4] => Array
                                                        (
                                                            [key] => _2274
                                                            [title] => Reseach
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

从那个数组我想要这个数组(下面):

Array
(
    [key] => _6
    [title] => <strong>face timeline</strong>
    [data] => Array
        (
            [url] => http://localhost/patient/face-timeline/
            [type] => content
            [cid] => 2291
            [timeline] => 0
        )

    [children] => Array
        (
            [0] => Array
                (
                    [key] => _2278
                    [title] => Post Op Visit
                )

            [1] => Array
                (
                    [key] => _2277
                    [title] => Surgery
                )

            [2] => Array
                (
                    [key] => _2276
                    [title] => Pre-Op
                )

            [3] => Array
                (
                    [key] => _2275
                    [title] => Consultation
                )

            [4] => Array
                (
                    [key] => _2274
                    [title] => Reseach
                )

        )

)  

这是我试过的

function recursion($array,$postid) {

    foreach ($array as $key=>$value) {

       if((isset($value['data']['cid'])) && ($value['data']['cid'] == $postid)){

            $tmp = $value;
                return $value;

        }
        if (is_array($value))
        {

            recursion($value,$postid); 
        } 
    }

}  

此功能不返回值 此处$postid2291。这是我正在搜索,我能够打印该阵列,但无法返回该值 这是link

2 个答案:

答案 0 :(得分:1)

如果您只想获得特定值,请使用:

   function recursive($your_array)
    {
        $newArray = [];

        foreach ($your_array as $key => $val) {
            if (array_keys($your_array) == 'children') {
                foreach($val as $key2 => $val3){
                    $newArray[] = recursive($val3);
                }
            }
        }
        print_r($newArray);
    }

答案 1 :(得分:0)

这将为您提供结果:

$searchedData = searchCustomRecursive('2291',$userdb);

function searchCustomRecursive($searchString, $array, $previousArray= Array()){
    if(is_Array($array)){
        $newArray = recursive_array_search($searchString,$array);
        if(is_Array($newArray)){
            searchCustomRecursive($searchString, $newArray,$array);
        }else{ 
            print_r($previousArray); // Check your result here...
            return $previousArray;
      }

    }
}

function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $value;
        }
    }
    return false;
}