【问题标题】:How to get particular nested array based on the given matched key?如何根据给定的匹配键获取特定的嵌套数组?
【发布时间】:2018-06-25 14:14:09
【问题描述】:

如何使用 PHP 内置函数根据给定的匹配键获取特定的嵌套数组

场景

$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'  => [

            ],
        ]
    ]
];

请注意,“id”和“children”键始终存在。如何获得“1035”ID的“孩子”..?

预期输出

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

试过

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;    

我用上面的代码试过了,但是 "if ($k == 'children') {" 不再匹配了..!

欢迎提出任何建议...(最喜欢 PHP 的内置函数!)

【问题讨论】:

  • 你不能得到父母,我的意思是......你可以得到[id: stuff],但不能得到[[id: stuff]]。可以吗?
  • @PraveenKumarPurushothaman 但我想要它的全部孩子,而不仅仅是像“[id: stuff]”。谢谢。
  • 好吧,看来你没听懂。我的意思是,我可以给你一个[...] 方式而不是[[...]] 方式...见家长... ☺
  • @PraveenKumarPurushothaman 你能在这里分享一些代码吗!
  • 你现在可以检查...

标签: php arrays multidimensional-array nested-loops


【解决方案1】:

我能够做到这一点。请检查代码中的cmets:

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

输出

检查 291
检查 291 的孩子
有孩子 291
检查 1034
检查孩子 1034
有孩子 1034
检查 111
检查孩子共 111 个
检查 1035
是的,我们找到了 1035
需要找到突破的方法!
检查 1024
检查儿童共 1024 个

找到了!
数组 ( [id] => 1035 [lft] => 左 [孩子] => 数组 ( [0] => 数组 ( [id] => 1036 [孩子] => 数组 ( [0] => 数组 ( [id] => 222 [someKey] => 一些值 [孩子] => 数组 ( ) ) ) ) [1] => 数组 ( [id] => 333 [someKey] => 一些值 [孩子] => 数组 ( ) ) ) )

演示:

【讨论】:

    【解决方案2】:

    您可以在其他检查中使用函数:

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

    【讨论】:

    • 谢谢。它也工作正常。但不想使用多个“foreach”循环..
    猜你喜欢
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2023-03-22
    • 2020-02-22
    • 2021-06-11
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多