我想用它的子句解析我的数组: 例如:
但是在我的代码中,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]