【问题标题】:PHP Adjacency List to Nested Array嵌套数组的 PHP 邻接表
【发布时间】:2012-03-22 06:40:29
【问题描述】:

我正在尝试使用 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"
    }
]

【问题讨论】:

  • 您能否向我们展示您调用 json_encode 的代码部分?
  • 它有效,但当我考虑它时,理论上它并没有

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


【解决方案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));

【讨论】:

    【解决方案2】:

    这里我假设您将最终结果存储在 $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}]
    

    【讨论】:

    • 因为这是嵌套数组,所以您的解决方案不会完全符合我的要求。它确实将顶级值转换为我看到的数组值。
    • 您的代码仍然不适用于嵌套数据。我拥有的数据在邻接列表中。
    • 好的,你能告诉我根据你的要求我的代码有什么问题吗?我刚才在猜测,所以你需要你告诉我出了什么问题:)
    • $refs[] = &$thisref;不处理子项。
    • 嗯:S,你能发布你从我上一个答案中得到的输出吗?
    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多