根据SQL查询返回的数据创建多维数组

时间:2019-02-24 21:44:15

标签: php sql pdo

我有一个查询要从数据库中获取数据:

$query = $pdo->prepare('SELECT AVA_Id, AVA_RoomId, AVA_Date, AVA_Status FROM ___Table');
$query->execute();

我正在使用它将所有数据放入一个数组:

while ($fetch = $query->fetch(PDO::FETCH_ASSOC)) {
    $array[] = [
        'AVA_Id' => $fetch['AVA_Id'],
        'AVA_RoomId' => $fetch['AVA_RoomId'],
        'AVA_Date' => $fetch['AVA_Date'],
        'AVA_Status' => $fetch['AVA_Status']
    ];
}

女巫给我以下数组:

Array
(
    [0] => Array
        (
            [AVA_Id] => 1
            [AVA_RoomId] => 47
            [AVA_Date] => 2019-02-24
            [AVA_Status] => Open
        )
    [1] => Array
        (
            [AVA_Id] => 2
            [AVA_RoomId] => 48
            [AVA_Date] => 2019-02-26
            [AVA_Status] => Open
        )
)

但是我如何将结果返回到更复杂的数组中呢?

Array
(
    [47] => Array
        (
            [2019-02-24] => Array
                (
                    [AVA_Id] => 1
                    [AVA_Status] => Open
                )
        )
    [48] => Array
        (
            [2019-02-26] => Array
                (
                    [AVA_Id] => 2
                    [AVA_Status] => Open
                )

        )
)

2 个答案:

答案 0 :(得分:1)

您可以按照以下步骤进行操作:

 predicted_utt_label[utt]=DA

答案 1 :(得分:0)

使用以下代码:

while ($fetch = $query->fetch(PDO::FETCH_ASSOC)) {
    $array[$fetch['AVA_RoomId']] = [
        $fetch['AVA_Date'] => [
            'AVA_Id' => $fetch['AVA_Id'],
            'AVA_Status' => $fetch['AVA_Status']
        ]
    ];
}

希望它对您有帮助。