获取多维数组php

时间:2019-05-13 19:40:34

标签: php arrays

我有一个带有子元素的多维数组,我想从数组中的每个父元素和子元素获取序号,格式为:

1.1.2-第一个问题,第一个孩子,第一个孩子的第一个孩子

2.1.2-第二个问题,第一个孩子,第一个孩子的第二个孩子

这是我正在使用的数组的递归函数

function buildTree(array $elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['parentId'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}

$tree = buildTree($questions);

这是数组的示例结果



Array
(
    [0] = Array
        (
            [id] = 156
            [question] = New Question
            [topicId] = 23
            [type] = 2
            [sortIndex] = 1
            [parentId] = 
            [parentOption] = 
            [children] =Array
                (
                    [0] = Array
                        (
                            [id] => 161
                            [question] = First Child to First Question
                            [topicId] = 23
                            [type] = 2
                            [sortIndex] = 2
                            [parentId] = 156
                            [parentOption] = Yes
                            [children] = Array
                                (
                                    [0] = Array
                                        (
                                            [id] = 163
                                            [question] = First Child to First Child
                                            [topicId] = 23
                                            [type] = 2
                                            [sortIndex] =; 5
                                            [parentId] = 161
                                            [parentOption] = Yes
                                        )

                                )

                        )

                    [1] = Array
                        (
                            [id] => 162
                            [question] = Second Child to first question
                            [topicId] =; 23
                            [type] = 2
                            [sortIndex] = 3
                            [parentId] = 156
                            [parentOption] = Yes
                            [children] = Array
                                (
                                    [0] = Array
                                        (
                                            [id] = 164
                                            [question] = First child to second child
                                            [topicId] = 23
                                            [type] = 2
                                            [sortIndex] = 4
                                            [parentId] = 162
                                            [parentOption] = Yes
                                        )

                                )

                        )

                )

        )



)

我正在使用此功能以递归方式获取所有项目。

function recursive($array, $level = 1){

    $i = 1;
    foreach($array as $key => $value){
        //If $value is an array.
        if(is_array($value)){
            //We need to loop through it.
            recursive($value, $level + 1);
        } else{
            //It is not an array, so print it out.
            echo str_repeat("-", $level), $value, '<br>';
        }

    }
}

任何想法如何实现我的目标?

0 个答案:

没有答案