Laravel Relation解析一个数组

时间:2017-09-29 07:24:29

标签: php arrays laravel parsing model

enter image description here我无法用正确的逻辑解析多维数组?

我想用它的子句解析我的数组: 例如:

  1. 1 parent-> 1child,
  2. 1父母 - > 2孩子等
  3. 2父母'没有任何孩子'
  4. 但是在我的代码中,else选项不起作用:

    foreach ($this->categories as $s) {
            echo "<hr>";
            echo "<p style='color:red;'>".$s."</p>";
            if ($s->childrenRecursive) {
                foreach ($s->childrenRecursive as $key) {
                    echo '<p>'.$s->id.''.$key->id.'</p>';
                    echo "it has many children";
                }   
            } else {
                echo $s->id;
                echo "single parent without any child";
            }
        }
    

    此代码的结果:

    {"id":13,"image":null,"parent_id":null,"top":0,"column":1,"sort_order":0,"status":1,"deleted_at":null,"created_at":"2017-09-25 12:24:11","updated_at":"2017-09-25 12:24:11","children_recursive":[{"id":15,"image":null,"parent_id":13,"top":0,"column":1,"sort_order":0,"status":1,"deleted_at":null,"created_at":"2017-09-25 12:26:09","updated_at":"2017-09-25 12:26:09","children_recursive":[]},{"id":16,"image":null,"parent_id":13,"top":0,"column":1,"sort_order":0,"status":1,"deleted_at":null,"created_at":"2017-09-25 12:36:43","updated_at":"2017-09-25 12:36:43","children_recursive":[]}]}
    

    1315

    它有很多孩子 1316

    它有很多孩子

    [![{"id":14,"image":null,"parent_id":null,"top":0,"column":1,"sort_order":0,"status":1,"deleted_at":null,"created_at":"2017-09-25 12:25:04","updated_at":"2017-09-25 12:25:04","children_recursive":\[\]}][1]][1]
    

0 个答案:

没有答案