Format my JSON string into an
    ordered list in PHP

后端 未结 5 889
抹茶落季
抹茶落季 2021-01-29 14:48

I\'m working on a simple CMS for a pet project. I currently have a JSON string that contains a list of page ID\'s and Parent page ID\'s for a menu structure.

I want to n

5条回答
  •  孤城傲影
    2021-01-29 15:04

     3,
        'children' => array(
            'id'        =>  4,
            'children'  =>  array(
                    'id'    =>  5,
                )
            )
    ));
    
    array_push($json_array, array('id' => 6));
    array_push($json_array, array('id' => 2));
    array_push($json_array, array('id' => 4));
    
    //your json object
    $json_object = json_encode($json_array);
    //echo $json_object;
    
    //here is where you decode your json object
    $json_object_decoded = json_decode($json_object,true);
    
    //for debug to see how your decoded json object looks as an array
    /*
    echo "
    ";
        print_r($json_object_decoded);
    echo "
    "; */ echo "
      "; foreach($json_object_decoded as $node){ if(isset($node['id'])){ echo "
    1. " . $node['id']; if(isset($node['children'])){ echo "
        "; echo "
      1. " . $node['children']['id'] . "
      2. "; if(isset($node['children'])){ echo "
          "; echo "
        1. " . $node['children']['children']['id'] . "
        2. "; echo "
        "; } echo "
      "; } echo "
    2. "; } } echo "
    "; ?>

提交回复
热议问题