首页 > 解决方案 > 在 PHP 中使动态树视图引导程序适应我的 BDD

问题描述

我对 php 数组有疑问。我终于想要一个这样的数组。这是我需要的:

$arr = [
    [
        'text' => '2018-11-01>123456>cassa',
        'nodes' => [
            [
                'text' => '32weds>has>aaaaa',
                'nodes' => [
                    [
                        'text' => '32.png',
                    ],
                ],
            ],
            [
                'text' => 'aegergert24562>han>asdfsd',
                'nodes' => [
                    [
                        'text' => '32(1).png',
                    ],
                ],
            ],

        ],
    ],
    [
        'text' => '2018-11-02>1234>cassa',
        'nodes' => [
            [
                'text' => 'qwerty>has>aaaaa',
                'nodes' => [
                    [
                        'text' => 'rr.png',
                    ],
                ],
            ],
            [
                'text' => 'sDFSDF>ha>aaaaa',
                'nodes' => [
                    [
                        'text' => 'ff.png',
                    ],

                ],
            ],

        ],

    ],
];

$data = $arr;
echo json_encode($data);

所以我的几个测试的php代码如下:

$query = " SELECT * FROM saisie order by date, n_trans, destination, n_fact, client, transitaire ";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
    $sub_data["code"] = $row["code"];
    $sub_data["text"] = $row["date"] . '>' . $row["n_trans"] . '>' . $row["destination"];
    $sub_data["text1"] = $row["n_fact"] . '>' . $row["client"] . '>' . $row["transitaire"];
    $sub_data["nom_doc"] = $row["nom_doc"];
    $data[] = $sub_data;
}
$output = array();
foreach ($data as &$value) {
    $id = $value['text'];
    $id1 = $value['text1'];
    if (!isset($id)) {
        if (!array_key_exists($value['text1'], $output)) {
            $output[$id1] = array(
                'text' => $id1,
                'nodes' => array()
            );
        }
        $output[$id1]['text'] = &$value;
    } else {
        if (!array_key_exists($value['text'], $output)) {
            $output[$id] = array(
                'text' => $id,
                'nodes' => array()
            );
        }
        $output[$id]['nodes'][$id1] = &$value;
    }
}

而且由于我的阵列不是很强大,所以我无法达到相同的结果。

标签: phparraystreeview

解决方案


推荐阅读