I can create simple json objects like this:
$d = array(\'item\' => \"$name\" ,\'rate\' => \"$rating\");
But what if I want to build an ar
But I want multiple json objects in a json array when i encode it.
Then create an array of arrays and pass it to json_encode
. The documentation about arrays explains how to add elements to an array, in the section Creating/modifying with square bracket syntax.
Associative arrays, like the one you already have, will be encoded as objects, "normal" arrays (arrays with consecutive numerical keys) will be encoded as arrays.
Example:
$d = array();
// This appends a new element to $d, in this case the value is another array
$d[] = array('item' => "$name" ,'rate' => "$rating");
$json = json_encode($d);