Get the Values from multidimensional array

时间:2023-01-20 12:12:00

I have the array structure as given below... Here I want to get the id value of all final child by dynamically because this array may have a lot of child. How I get id value of every final child by dynamically?
I want to values from below array structure like this

我有下面给出的数组结构...这里我想动态获取所有最终子的id值,因为这个数组可能有很多子。我如何通过动态获得每个最终孩子的id值?我想从这样的数组结构下面的值

6
7
8
9
17

Array
(
    [0] => Array
        (
            [id] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 5
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 8
                        )

                    [1] => Array
                        (
                            [id] => 9
                        )

                )

        )

    [2] => Array
        (
            [id] => 3
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                        )

                    [1] => Array
                        (
                            [id] => 16
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 17
                                        )

                                )

                        )

                )

        )

)

2 个解决方案

#1


1  

This will return the endpoints with recursion

这将返回带递归的端点

function getIds($data){
    $out = array();
    if(is_array($data)){
            foreach($data as $elm){
                    if($elm['children']){
                            $out = array_merge($out,getIds($elm['children']));
                    }else{
                            $out[] = $elm['id'];
                    }
            }
    }
    return $out;
}

#2


1  

Easy to do with recursion:

递归很容易:

function get_final($arr) {
    $out = array();
    if(is_array($arr)) {
        foreach($arr as $a) {
            $out = array_merge($out, get_final($a));
        }
        return $out;
    }
    else return array($arr);
}

#1


1  

This will return the endpoints with recursion

这将返回带递归的端点

function getIds($data){
    $out = array();
    if(is_array($data)){
            foreach($data as $elm){
                    if($elm['children']){
                            $out = array_merge($out,getIds($elm['children']));
                    }else{
                            $out[] = $elm['id'];
                    }
            }
    }
    return $out;
}

#2


1  

Easy to do with recursion:

递归很容易:

function get_final($arr) {
    $out = array();
    if(is_array($arr)) {
        foreach($arr as $a) {
            $out = array_merge($out, get_final($a));
        }
        return $out;
    }
    else return array($arr);
}