When you use MySQL 8, you can use the json_table
expression like this:
<?php $array = [ ['id' => 1, 'name' => 'one'], ['id' => 2, 'name' => 'two'], ['id' => 3, 'name' => 'three'] ]; $data = json_encode($array); $sql = "SELECT tbl.* FROM JSON_TABLE( '{\"data\":$data}', '$.data[*]' COLUMNS ( id VARCHAR(40) PATH '$.id', name VARCHAR(100) PATH '$.name') ) tbl"; echo $sql;
In MySQL 8.0, the results of the above query are as follows:
+====+=======+ | id | name | +====+=======+ | 1 | one | +----+-------+ | 2 | two | +----+-------+ | 3 | three | +----+-------+