PHP二维数组转json?

格式要求:[{
title: '早餐'
, id: 1
, children: [{
title: '油条'
, id: 5
}, {
title: '包子'
, id: 6
}, {
title: '豆浆'
, id: 7
}]
}]
我的:array(5) { [0]=> array(3) { ["id"]=> int(1) ["title"]=> string(12) "文章管理" ["children"]=> array(2) { ["id"]=> int(3) ["title"]=> string(12) "文章列表" } } [1]=> array(3) { ["id"]=> int(10) ["title"]=> string(12) "用户管理" ["children"]=> array(2) { ["id"]=> int(11) ["title"]=> string(12) "会员列表" } } [2]=> array(3) { ["id"]=> int(13) ["title"]=> string(12) "商品管理" ["children"]=> array(2) { ["id"]=> int(14) ["title"]=> string(12) "商品列表" } } [3]=> array(3) { ["id"]=> int(15) ["title"]=> string(12) "管理权限" ["children"]=> array(2) { ["id"]=> int(17) ["title"]=> string(15) "管理员列表" } } [4]=> array(2) { ["id"]=> int(18) ["title"]=> string(12) "网站设置" } }

json_encode后://children里少了[]号
[{"id":1,"title":"文章管理","children":{"id":3,"title":"文章列表"}},
{"id":10,"title":"用户管理","children":{"id":11,"title":"会员列表"}},
{"id":13,"title":"商品管理","children":{"id":14,"title":"商品列表"}},{"id":15,"title":"管理权限","children":{"id":17,"title":"管理员列表"}},{"id":18,"title":"网站设置"}]
最新回答
海心

2024-11-07 13:22:54

children里少了[]号 得原因是
[]是用来包含数组用的,children如果只是个一维数组的话就是{}包括着,
只有children里面包含的是个二维数据才会用[]包着
比如这个
[{"id":1,"title":"文章管理","children":[{"id":3,"title":"文章列表"}]},
children decode出来就是个二维数组