php - PHP多级别JSON响应到单级别 array ( 递归)

  显示原文与译文双语对照的内容



我希望有人能帮助我解决递归问题。 我有以下JSON响应,我希望它在一个级别为 en id_parent的单级 array 中。

的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
 )
)

这是我尝试的,但它给了我一个多维的array 。


$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;
}

我希望有人能帮助我。 我就是找不到。

先谢谢了。

时间: 原作者:

这应该是非常自我解释的。 只要使用 static array,并在你运行所有内容时构建它。


例如:


function arrangeCategories ($categories, $parent = null)
{
 static $result = array();

 foreach ($categories as $id => $category) {
 $result[] = array(
 'id' => $id,
 'id_parent' => $parent,
 'name' => $category['name'],
 'count' => $category['count']
 );

 if (isset($category['childeren'])) {
 arrangeCategories($category['childeren'], $id);
 }
 }

 return $result;
}


$categories = arrangeCategories(json_decode($response, true));

var_export($categories);


输出:


array (
 0 =>
 array (
 'id' => 424,
 'id_parent' => NULL,
 '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' => NULL,
 'name' => 'Category 2',
 'count' => 45,
 ),
)


( 作为一个局外人"子children在示例json中拼写错误。 我把它保持在这里保持一致。

原作者:
...