user5147795
user5147795

Reputation:

get specific value from multidimensional array

I have big multidimensional array. And i have to find specific sub-array from it.
I tried using one recursion function but it actually not returning values. Can anyone give me another solution.
Here is the preview of 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
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

From that array i want this array(below):

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
                )

        )

)  

Here is what i tried

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); 
        } 
    }

}  

This function is not returning values.
Here $postid is 2291. That is i am searching and i am able to print that array but can't able to return the value Here is link

Upvotes: 6

Views: 1107

Answers (2)

Shri.harry
Shri.harry

Reputation: 287

This will give you your result:

$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;
}

Upvotes: 0

aldrin27
aldrin27

Reputation: 3407

If you want to get only a specific value use this:

   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);
    }

Upvotes: 1

Related Questions