Php 我需要获取深度未定义的数组的值

Php 我需要获取深度未定义的数组的值,php,arrays,recursion,Php,Arrays,Recursion,我有一个数组,它有子元素,可以有更多的子元素,等等。我需要所有父母和孩子的身份证 我尝试用一个调用自身的函数遍历数组,但它不起作用。我试过:(初始树是前面提到的数组) 它返回数组{[0]=>int(3)[1]=>int(4)[2]=>int(5)} 下面是一个输入数组示例。在该示例中,我需要一个具有所有id的数组,例如$values=[3,4,5,7,8,9,10,11] `array(5) { ["id"]=> int(3) ["children"]=>

我有一个数组,它有子元素,可以有更多的子元素,等等。我需要所有父母和孩子的身份证

我尝试用一个调用自身的函数遍历数组,但它不起作用。我试过:(初始树是前面提到的数组)

它返回
数组{[0]=>int(3)[1]=>int(4)[2]=>int(5)}

下面是一个输入数组示例。在该示例中,我需要一个具有所有id的数组,例如
$values=[3,4,5,7,8,9,10,11]

`array(5) {
    ["id"]=>
    int(3)
    ["children"]=>
    array(2) {
        [0]=>
        array(5) {
            ["id"]=>
            int(4)
            ["children"]=>
            array(2) {
                [0]=>
                array(5) {
                    ["id"]=>
                    int(5)
                    ["children"]=>
                    array(0) {
                    }
                }
                [1]=>
                array(5) {
                    ["id"]=>
                    int(7)
                    ["children"]=>
                    array(0) {
                    }
                }
            }
        }
        [1]=>
        array(5) {
            ["id"]=>
            int(8)
            ["children"]=>
            array(3) {
                [0]=>
                array(5) {
                    ["id"]=>
                    int(9)
                    ["children"]=>
                    array(0) {
                    }
                }
                [1]=>
                array(5) {
                    ["id"]=>
                    int(10)
                    ["children"]=>
                    array(0) {
                    }
                }
                [2]=>
                array(5) {
                    ["id"]=>
                    int(11)
                    ["children"]=>
                    array(0) {
                    }
                }
            }
        }
    }
}

您的代码有几个问题:

  • 返回foreach循环内部,因此它只执行一次
  • 结构有点不一致,因为外部数组有一个id,但它是一个子数组,并且没有一个“子”数组有id
  • 因此,您只需要添加id(如果它存在),而不是在循环中返回,将每个子级的id添加到一个数组中,并将它们合并在一起以获得返回的值

    function get_all_ids($array) {
    
        // add id conditionally
        $ids = isset($array['id']) ? [[$array['id']]] : [];
    
        // accumulate ids from all children recursively
        // note that you don't need an if here. If children is empty the loop won't execute
        foreach ($array['children'] as $child) {
            $ids[] = get_all_ids($child);
        }
    
        // return merged results
        return array_merge(...$ids);
    }
    

    你试过什么代码?您提到了一个不起作用的递归函数,您应该提供该代码。您应该提供您尝试过的代码,但是,您希望如何返回值?在另一个数组中?你需要知道这个ID是属于孩子还是父母的吗?@JordiCastillo这是不是意味着你想把所有的ID都放在电脑上array@HamzaNig正确,在本例中,我需要值为3,4,5,7,8,9,10,11的数组是的,除非您使用的是PHP5,在这种情况下,该运算符不可用,您需要使用
    return call\u user\u func\u数组('array_merge',$id);
    function get_all_ids($array) {
    
        // add id conditionally
        $ids = isset($array['id']) ? [[$array['id']]] : [];
    
        // accumulate ids from all children recursively
        // note that you don't need an if here. If children is empty the loop won't execute
        foreach ($array['children'] as $child) {
            $ids[] = get_all_ids($child);
        }
    
        // return merged results
        return array_merge(...$ids);
    }