{ "label": "Devices per year", "data": [ [1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9] ] }
After several attempt I didn't find the solution. For example I tried this:
$arrayDateAndMachine = array( "1999"=>3.0, "2000"=>3.9 ); $arr = array( "label" => "Devices per year", "data" => $arrayDateAndMachine ); var_dump(json_encode($arr));
The best way to create a JSON object is to start from a PHP array. The reason is that PHP arrays are a perfect match for the JSON structure: each PHP array key => value pair becomes a key => value pair inside the JSON object. The $json variable looks like this: The variable to be encoded as a JSON object. ( $array, in the previous example).
PHP File explained: 1 Convert the request into an object, using the PHP function json_decode (). 2 Access the database, and fill an array with the requested data. 3 Add the array to an object, and return the object as JSON using the json_encode () function. More ...
The best way to create a JSON object is to start from a PHP array. The reason is that PHP arrays are a perfect match for the JSON structure: each PHP array key => value pair becomes a key => value pair inside the JSON object. The $json variable looks like this: json_encode () takes three arguments:
json_decode (), as its name suggests, decodes a JSON string into a PHP object or array. All the variables contained in the JSON object will be available in the PHP object or array. Here is how it works. Let’s take our first JSON object example:
$obj = new stdClass(); $obj->label="Devices per year"; $obj->data = array( array('1999','3.0'), array('2000','3.9'), //and so on... ); echo json_encode($obj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With