简体   繁体   中英

How to get particular nested array based on the given matched key?

How to get particular nested array based on the given matched key using PHP built in function

Scenario

$id = 1035; // Searching ID

$a = [
    'id'=> 291,    
    'children' => [
        [
            'id'        => 1034,
            'children'  => [
                [
                    'id'      => 111,
                    'name'    => 'ABC',
                    'figure'  => '6 digits',  
                    'children'=> []  
                ],
                [
                    'id'        => 1035,
                    'lft'       => 'LEFT',
                    'children'  => [
                        [
                            'id'        => 1036,
                            'children'  => [
                                [
                                    'id'      => 222,
                                    'someKey' => 'some value',
                                    'children'=> []  
                                ]
                            ]
                        ],
                        [
                            'id'      => 333,
                            'someKey' => 'some value',
                            'children'=> []  
                        ]
                    ],
                ]
            ],
         ],
         [
            'id'        => 1024,
            'title'     => 'ABC',    
            'children'  => [

            ],
        ]
    ]
];

Please note, 'id' & 'children' keys are always be there. How to get the "children" of "1035" ID..?

Expected Output

[
    [
        'id'        => 1036,
        'children'  => [
            [
                'id'      => 222,
                'someKey' => 'some value',
                'children'=> []  
            ]
        ],
    ],
    [
        'id'      => 333,
        'someKey' => 'some value',
        'children'=> []  
    ]
];

Tried

function getRecursiveCategoryIds($key, $categories = []){
    $return = null;
    try {
        array_walk_recursive($categories, function($v, $k) use ($key, &$return){

            if (null != $return) {

                 // Run loop to get the next immediate "children" key
                 if ($k == 'children') {  // It's not matching anymore

                     $return = $v;
                     //return false;
                     throw new Exception;

                 }
             } else if($v == $key) {
                 // Found
                 $return = $v;
             }
        });
    } catch(Exception $e) {}

    return $return;
}

$d = getRecursiveCategoryIds($id, $a);
echo '<pre>D: '; print_r($d); die;    

I tried by the above code, but the "if ($k == 'children') {" is not matched any more..!

Any suggestions are welcome... (PHP's Built in function is most prefer!)

I was able to do this. Please check the comments in the code:

<?php
    $id = 1035; // Searching ID
    $myObj = array();

    $a = [
        'id'=> 291,    
        'children' => [
            [
                'id'        => 1034,
                'children'  => [
                    [
                        'id'      => 111,
                        'name'    => 'ABC',
                        'figure'  => '6 digits',  
                        'children'=> []  
                    ],
                    [
                        'id'        => 1035,
                        'lft'       => 'LEFT',
                        'children'  => [
                            [
                                'id'        => 1036,
                                'children'  => [
                                    [
                                        'id'      => 222,
                                        'someKey' => 'some value',
                                        'children'=> []  
                                    ]
                                ]
                            ],
                            [
                                'id'      => 333,
                                'someKey' => 'some value',
                                'children'=> []  
                            ]
                        ],
                    ]
                ],
            ],
            [
                'id'        => 1024,
                'title'     => 'ABC',    
                'children'  => [

                ],
            ]
        ]
    ];

    function findObject($id, $obj) {
        global $myObj;
        // This is an object.
        if (isset($obj["id"])) {
            echo "Checking {$obj["id"]}<br />";
            // Check the id to what we need.
            if ($obj["id"] == $id) {
                // Yay! We found it. Return the object.
                echo "Yay we found {$obj["id"]}<br />";
                $myObj = $obj;
            }
            else {
                echo "Checking children of {$obj["id"]}<br />";
                // See if it has any children
                if (isset($obj["children"]) && count($obj["children"]) > 0) {
                    echo "There are children for {$obj["id"]}<br />";
                    foreach ($obj["children"] as $child) {
                        findObject($id, $child);
                    }   
                }
            }
        }
    }

    findObject($id, $a);
    print_r($myObj);

Output

Checking 291
Checking children of 291
There are children for 291
Checking 1034
Checking children of 1034
There are children for 1034
Checking 111
Checking children of 111
Checking 1035
Yay we found 1035
Need to find a way to break out!
Checking 1024
Checking children of 1024

Found it!
Array ( [id] => 1035 [lft] => LEFT [children] => Array ( [0] => Array ( [id] => 1036 [children] => Array ( [0] => Array ( [id] => 222 [someKey] => some value [children] => Array ( ) ) ) ) [1] => Array ( [id] => 333 [someKey] => some value [children] => Array ( ) ) ) )

Demo:

You can use function inside other check :

$id=1035;
$a = [
    'id'=> 291,    
    'children' => [
        [
            'id'        => 1034,
            'children'  => [
                [
                    'id'      => 111,
                    'name'    => 'ABC',
                    'figure'  => '6 digits',  
                    'children'=> []  
                ],
                [
                    'id'        => 1035,
                    'lft'       => 'LEFT',
                    'children'  => [
                        [
                            'id'        => 1036,
                            'children'  => [
                                [
                                    'id'      => 222,
                                    'someKey' => 'some value',
                                    'children'=> []  
                                ]
                            ]
                        ],
                        [
                            'id'      => 333,
                            'someKey' => 'some value',
                            'children'=> []  
                        ]
                    ],
                ]
            ],
         ],
         [
            'id'        => 1024,
            'title'     => 'ABC',    
            'children'  => [

            ],
        ]
    ]
];


function nigsearch($arr,$id)
{
    if(gettype($arr) == 'array')
    foreach($arr as $key =>$list)
{

    if(gettype($list) == 'array'){
    if(isset($list['id']))
    {
        if($list['id'] ==$id)
            print_r($list['children']);


    }
        nigsearch($list,$id);
    }
}

}

foreach($a as $key)
{

    nigsearch($key,$id);


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM