0

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

2 回答 2

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));
于 2012-03-22T06:51:44.713 回答
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}]
于 2012-03-22T06:45:24.613 回答