PHP对单级数组的多级JSON响应(递归)

时间:2014-08-29 20:24:08

标签: php arrays json recursion categories



我希望有人能帮我解决递归问题。我有以下JSON响应,我希望它在id en id_parent的单级数组中。

JSON响应:

{
    "424":{
        "name":"Category 1",
        "count":4425,
        "childeren":{
            "462":{
                "name":"Category 1.1",
                "count":1231,
                "childeren":{
                    "574":{
                        "name":"Category 1.1.1",
                        "count":527
                    },
                    "581":{
                        "name":"Category 1.1.2",
                        "count":175
                    },
                    "583":{
                        "name":"Category 1.1.3",
                        "count":153
                    }
                }
            },
            "432":{
                "name":"Category 1.2",
                "count":451,
                "childeren":{
                }
            },
            "132":{
                "name":"Category 1.3",
                "count":41,
                "childeren":{
                }
            }
        }
    },
    "125":{
        "name":"Category 2",
        "count":45,
        "childeren":{
        }
    }
}

我想得到的结果是:

Array
(
    [0] => Array
        (
            [id] => 424
            [id_parent] => 
            [name] => Category 1
            [count] => 4425
        )
    [1] => Array
        (
            [id] => 462
            [id_parent] => 424
            [name] => Category 1.1
            [count] => 1231
        )
    [2] => Array
        (
            [id] => 574
            [id_parent] => 462
            [name] => Category 1.1.1
            [count] => 527
        )
    [3] => Array
        (
            [id] => 581
            [id_parent] => 462
            [name] => Category 1.1.2
            [count] => 175
        )
    [4] => Array
        (
            [id] => 583
            [id_parent] => 462
            [name] => Category 1.1.3
            [count] => 153
        )
    [5] => Array
        (
            [id] => 432
            [id_parent] => 424
            [name] => Category 1.2
            [count] => 451
        )
    [6] => Array
        (
            [id] => 132
            [id_parent] => 424
            [name] => Category 1.3
            [count] => 41
        )
    [7] => Array
        (
            [id] => 125
            [id_parent] => 
            [name] => Category 2
            [count] => 45
        )
)

这是我试过的,但它给了我一个多维数组。

$categoryObj = json_decode( $response, true );
$categories = getCategory( $categoryObj);

function getCategory( $categoryObj, $id_parent='', $result=array() ) {
    foreach( $categoryObj as $id_categorie => $category ) {

        if( isset($category['childeren']) && is_array($category['childeren']) ) {
            $result[] = getCategory( $category['childeren'], $id_categorie, $result );
        } else {
            $result[] = array( 
                'id' => $id_categorie,
                'id_parent' => $id_parent
            );
        }
    }
    return $result;
}

我希望有人可以帮助我。我无法做对
提前谢谢。

0 个答案:

没有答案