相关疑难解决方法(0)

将PHP数组转换为JSON树

我有这种格式的数组:

array(
    array('id' => 1, 'parent_id' => null, 'name' => 'lorem ipsum'),
    array('id' => 2, 'parent_id' => 1, 'name' => 'lorem ipsum1'),
    array('id' => 3, 'parent_id' => 1, 'name' => 'lorem ipsum2'),
    array('id' => 4, 'parent_id' => 2, 'name' => 'lorem ipsum3'),
    array('id' => 5, 'parent_id' => 3, 'name' => 'lorem ipsum4'),
    array('id' => 6, 'parent_id' => null, 'name' => 'lorem ipsum5'),
);
Run Code Online (Sandbox Code Playgroud)

我必须使用以下样式将此数组转换为json对象:

var json = {  
    id: "1",  
    name: "loreim ipsum",  
    data: {},  
    children: [{  
        id: "2",  
        name: …
Run Code Online (Sandbox Code Playgroud)

php json parent-child

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

json ×1

parent-child ×1

php ×1