I can create simple json objects like this:
$d = array(\'item\' => \"$name\" ,\'rate\' => \"$rating\");
But what if I want to build an ar
This will create a multi-dimensional array from your database query, and then encode it as JSON.
$d = array();
while ($row = $stmt->fetch_assoc()) {
$d[] = $row;
}
$json = json_encode($d);
Each $row
will be an associative array of the data returned from the database. Assigning it to $d[]
adds it as an indexed element of that container array.