如何使用PHP处理任何数量的嵌套项目的Nestable输出?

时间:2014-03-17 16:35:04

标签: php json

我正在尝试正确读取我用于菜单编辑器的插件的输出。输出以JSON字符串的形式出现。

示例JSON数据:

[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]}]

这是插件的实际应用:http://jsfiddle.net/Av8q5/(目前上限为5级)

我正在使用PHP尝试正确遍历任意数量的嵌套项目,但我只是不确定如何执行此操作。我这样做的尝试(但我认为它需要工作)只有2个级别,但我需要根据任意数量的嵌套项目进行扩展。我对使用PHP管理JSON字符串比较陌生,但这是我尝试获取所有ID(我的菜单代码也包含数据内容,而不仅仅是数据ID,如上例所示):

//Sample data from above
$data = '[{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]}]';

//Decode the JSON string to build the menu                                  
$menu = json_decode($data, true); 

//Store all of the ID's in an array
$ids = array();

//Hold the parent id to target this key when adding children to it
$y = 0;

//Start the loop through the menu array
foreach ($menu as $parentHolder) {                                          
    foreach ($parentHolder as $parentKey => $parent) {

        //Checks to see when this jumps into the children array
        if (!is_array($parent)) {                       

            //Only run on the content key                                   
            if ($parentKey == "id") {

                if ($parent) { $ids[] = $parent; $y++; } //Only to array if not empty               

            } //end check for content as key for the parents                                                    

        } else {                                                        

            //This is an array, which means this parent has children                                                                                                    
            foreach ($parent as $childHolder) {                                                     
                foreach ($childHolder as $childKey => $child) { 

                    //Only run on the content key
                    if ($childKey == "id") {

                        if ($child) { $ids[$y][] = $child; } //Only to array if not empty

                    } //end check for content as key for the children

                } //End child foreach loop                                                                                                      
            } //end childholder foreach loop

        } //Check if key is = content

    } //end Parent loop                                                                                                                                                         
} //end the main loop       

输出(基于上面的JSON示例):

在再次查看此输出时,我甚至认为这是正确的...我认为子数组应该附加到15,而不是单独添加到下一个键中。

Array
(
    [0] => 13
    [1] => 14
    [2] => 15
    [3] => Array
        (
            [0] => 16
            [1] => 17
            [2] => 18
        )

)

我正在尝试做的是正确循环一些像这样的JSON ......

示例:

[{"id":13,"children":[{"id":20,"children":[{"id":21},{"id":22}]}]},{"id":15,"children":[{"id":16,"children":[{"id":18},{"id":14},{"id":19}]},{"id":17},{"id":23}]}]

在这个例子中,孩子们中有孩子,如何在PHP中将其正确地插入到嵌套数组中,同时记住这个菜单可以深入到10个嵌套列表?

2 个答案:

答案 0 :(得分:1)

一种解决方案是编写递归函数。一个自称的函数。这是使用示例代码的粗略示例。我把你的输出代码分成了一个函数。如果嵌套内容是一个数组,只需调用该函数并将嵌套数据作为参数传入。

function output( $menu ) 
{
    //Start the loop through the menu array
    foreach ($menu as $parentHolder) {                                          
        foreach ($parentHolder as $parentKey => $parent) {

            //Checks to see when this jumps into the children array
            if (!is_array($parent)) {                       

                //Only run on the content key                                   
                if ($parentKey == "id") {

                    if ($parent) { $ids[] = $parent; $y++; } //Only to array if not empty               

                } //end check for content as key for the parents                                                    

            } else {                                                        

                //This is an array, which means this parent has children                                                                                                    
                foreach ($parent as $childHolder) {                                                     
                    foreach ($childHolder as $childKey => $child) { 

                        // recursive function call here
                        output( $child )

                        } //end check for content as key for the children

                    } //End child foreach loop                                                                                                      
                } //end childholder foreach loop

            } //Check if key is = content

        } //end Parent loop                                                                                                                                                         
    } //end the main loop       
}

答案 1 :(得分:0)

朋友您好,一个方便的代码

$d=json_decode('[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6},{"id":7},{"id":8}]},{"id":9},{"id":10}]},{"id":11},{"id":12}]');
output($d);
function output($menu,$parent=0)
{
    foreach ($menu as $handle)
    {
        if(isset($handle->children))
        {
            echo 'node : '.$handle->id.' - Pid : '.$parent.'<br>';
            // Insert into DataBase Code (menuId = $handle->id and ParentId = $parent)
            output($handle->children,$handle->id); 
        }
        else if(isset($handle->id))
        {
            echo 'node : '.$handle->id.' - Pid : '.$parent.'<br>';
            // Insert into DataBase Code (menuId = $handle->id and ParentId = $parent)
        }
    }

}

// print

节点:1 - pid:0

节点:2 - pid:0

节点:3 - pid:2

节点:4 - pid:2

节点:5 - pid:2

节点:6 - pid:5

节点:7 - pid:5

节点:8 - pid:5

节点:9 - pid:2

节点:10 - pid:2

节点:11 - pid:0

节点:12 - pid:0