PHP邻接列表到嵌套数组

时间:2012-03-22 06:40:30

标签: php arrays nested hierarchical-data adjacency-list

我正在尝试使用PHP将以下表数据转换为嵌套数组。我差不多完成了但仍然坚持了一下。

id  name                    parent
1   Apparel                                                                   
2   Appliances                                                                
46  Apparel                 1                                                 
47  Child Apparel           46                                                
49  Child Apparel 2         47        

使用此代码

$cats = array(); // from database

$refs = array();
$rcats = array();

foreach ($cats as $cat) {
    $thisref = &$refs[$cat['id']];

    $thisref['id'] = $cat['id'];
    $thisref['name'] = $cat['name'];
    $thisref['leaf'] = "true";

    if (!$cat['parent']) {
        $rcats[$cat['id']] = &$thisref;
    } else {
        unset($refs[$cat['parent']]['leaf']);
        $refs[$cat['parent']]['items'][$cat['id']] = &$thisref;
    }
}          

print_r(json_encode($rcats));                             

这导致以下JSON。

{
    "1": {
        "id": "1",
        "name": "Apparel",
        "items": {
            "46": {
                "id": "46",
                "name": "Apparel",
                "items": {
                    "47": {
                        "id": "47",
                        "name": "Child Apparel",
                        "items": {
                            "49": {
                                "id": "49",
                                "name": "Child Apparel 2",
                                "leaf": "true"
                            }
                        }
                    }
                }
            }
        }
    },
    "2": {
        "id": "2",
        "name": "Appliances",
        "leaf": "true"
    }
}

我想要JSON的地方如

[
    {
        "id": "1",
        "name": "Apparel",
        "items": [
            {
                "id": "46",
                "name": "Apparel",
                "items": [
                    {
                        "id": "47",
                        "name": "Child Apparel",
                        "items": [
                            {
                                "id": "49",
                                "name": "Child Apparel 2",
                                "leaf": "true"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id": "2",
        "name": "Appliances",
        "leaf": "true"
    }
]

2 个答案:

答案 0 :(得分:1)

我错了吗? :P

$cats      = array(); // From Database, but i use this
$cats      = array(
    array(
        'id'        => 1,
        'name'      => 'Jakarta',
        'parent'    => NULL
    ),
    array(
        'id'        => 2,
        'name'      => 'Bandung',
        'parent'    => 1
    ),
    array(
        'id'        => 3,
        'name'      => 'Surabaya',
        'parent'    => 1
    ),
    array(
        'id'        => 4,
        'name'      => 'Bali',
        'parent'    => NULL
    ),
    array(
        'id'        => 5,
        'name'      => 'Batam',
        'parent'    => NULL
    ),
);
$refs        = array();
$rcats   = array();

foreach ($cats as $cat) {
    $thisref            = &$refs[$cat['id']];
    $thisref['id']  = $cat['id'];
    $thisref['name']    = $cat['name'];
    $thisref['leaf']    = "true";

    if (!$cat['parent']) {
        $rcats[] = &$thisref;
    } 
    else {
        unset($refs[$cat['parent']]['leaf']);
        $refs[$cat['parent']]['items'][] = &$thisref;
    }
}      

/*
// IF YOU NEED CHANGE TO OBJECT

$rcats = (object)$rcats;
if(isset($rcats->items)){
    $rcats->items = (object)$rcats->items;
}
*/

header('Content-type: application/json');
print_r(json_encode($rcats));

答案 1 :(得分:0)

这里我假设您将最终结果存储在$ refs

print_r(json_encode(array_values($rcats)));

如果要取出所有索引,请在for循环中执行[]而不是id:

if (!$cat['parent']) {
    $rcats[] = &$thisref;
} else {
    unset($refs[$cat['parent']]['leaf']);
    $refs[$cat['parent']]['items'][] = &$thisref;
}

我抛出一个简单的例子:

<?php

$a = array('1' => array('a' => 3), '2' => array('b' => 4));
echo json_encode($a) . "\n";
echo json_encode(array_values($a)) . "\n";

输出:

{"1":{"a":3},"2":{"b":4}}
[{"a":3},{"b":4}]