php - Create dynamic DataTable from database

one text

I have this html table, I saved it to my database per column.And now I want to re-create it again using DataTables. Here are the list that I retrieved from my database. I added index to determine each row.

[
    {
        "data_key": "Product",
        "data_value": "Chair",
        "index": "0"
    },
    {
        "data_key": "Model",
        "data_value": "CH001",
        "index": "0"
    },
    {
        "data_key": "Quantity",
        "data_value": "12",
        "index": "0"
    },
    {
        "data_key": "Product",
        "data_value": "Table",
        "index": "1"
    },
    {
        "data_key": "Model",
        "data_value": "TB001",
        "index": "1"
    },
    {
        "data_key": "Quantity",
        "data_value": "5",
        "index": "1"
    },  
]

And here is what I want it to be. To be able to use it in DataTables as dataset.

[
    [
        "Chair",
        "CH001",
        "12",
    ],
    [
        "Table",
        "TB001",
        "5",
    ]
]

How to do this using loop in php? And also even if the list from database was not sorted by index.

Source