1

这是我关于 SO 的第一篇文章,所以如果这是这个问题的错误区域,我深表歉意。

我有一个用 PHP 编写的页面,我正在尝试使用名为jqTree的 jquery 插件创建具有特定布局的树。我正在使用以下返回格式从 SQL Server 2008 R2 数据库中提取页面加载数据:

╔═════════════╦═══════╦═════════╗
║  Location   ║  SAN  ║  Switch ║
╠═════════════╬═══════╬═════════╣
║ LocationA   ║ SAN1  ║ Switch1 ║
║ LocationA   ║ SAN1  ║ Switch2 ║
║ LocationA   ║ SAN2  ║ Switch3 ║
║ LocationB   ║ SAN3  ║ Switch4 ║
║ LocationB   ║ SAN3  ║ Switch5 ║
║ LocationB   ║ SAN3  ║ Switch6 ║
╚═════════════╩═══════╩═════════╝

我需要树看起来像:

LocationA
    |_ SAN1
    |    |_ Switch1
    |    |_ Switch2
    |_ SAN2
        |_ Switch3

LocationB
    |_ SAN3
         |_ Switch4
         |_ Switch5
         |_ Switch6

根据 jqTree 站点,JSON 必须采用以下格式(我假设):

var data = [
{
    label: 'LocationA',
    children: [
        { label: 'SAN1',
          children: [
               { label: 'Switch1'},
               { label: 'Switch2'}
              ]
        },
        { label: 'SAN2',
          children: [
               { label: 'Switch3'}
              ]
        }
},
{
    label: 'LocationB',
    children: [
        { label: 'SAN1',
          children: [
               { label: 'Switch4'},
               { label: 'Switch5'},
               { label: 'Switch6'}
              ]
}
}
];

我已经尝试过接近这样的事情:

$locations = array();

foreach ($results as $res) {
if(!in_array($res['town'], $locations, true)){

    $locations[$res['town']]['children'][$res['san']][] = $res['name'];
}
}

但这显然是不正确的...

我查看了嵌套数组解决方案,但有些东西对我来说并没有点击,所以我希望有人可以帮助我解决这个问题。我可以使用另一个插件,只要我可以拥有相同/相似的功能。

希望我能够很好地描述这种情况,但如果您需要更多数据,请告诉我。

提前致谢!

4

1 回答 1

1

这是创建示例 JSON 的数组:

$data = array(
    array(
        'label' => 'LocationA',
        'children' => array(
            array(
                'label' => 'SAN1',
                'children' => array(
                    array('label' => 'Switch1'),
                    array('label' => 'Switch2')
                )
            ),
            array(
                'label' => 'SANS',
                'children' => array(
                    array('label' => 'Switch3')
                )
            )
        )
    ),
    array(
        'label' => 'LocationB',
        'children' => array(
            array(
                'label' => 'SAN1',
                'children' => array(
                    array('label' => 'Switch4'),
                    array('label' => 'Switch5'),
                    array('label' => 'Switch6')
                )
            )
        )
    )
);

echo json_encode($data);

我相信这应该让你的数据变成这种格式:

$locations = array();

foreach ($results as $res) {
    $locations[ $res['town'] ][ $res['san'] ][] = $res['name'];
}

$data = array();
foreach ($locations as $location => $sans) {
    $location_array = array('label' => $location);
    foreach ($sans as $san => $names) {
        $san_array = array('label' => $san);
        foreach ($names as $name) {
            $san_array['children'][] = array('label' => $name);
        }
        $location_array['children'][] = $san_array;
    };
    $data[] = $location_array;
}

echo json_encode($data);
于 2014-03-29T05:53:02.740 回答