How can i make the JSON based array in PHP
-
10-12-2019 - |
Question
I would like to know how can we make the JSON based array using json_encode() PHP ; The format of the array should be look like this.
callback([{"ProductID":1,"ProductName":"Chai","UnitPrice":18,"UnitsInStock":39,"Discontinued":false}])
When we need the this header("Content-type: application/json");
UPDATE
in above format you can see the format check the numeric values, json_encode not mention the double quotes the numeric values. I just want to configure the Mysql values on this format, like numeric value without double quotes, and string values with double quotes. We should have to configure mysql values on this format.
Thanks
Solution
Example:
$array = array(
'ProductID' => 1,
'ProductName' => 'Chai',
'UnitPrice' => 18,
'UnitsInStock' => 39,
'Discontinued' => false
);
header("Content-type: application/json");
echo json_encode($array);
OTHER TIPS
edit: It appears you are trying to serve JSONP, not JSON. JSONP should be given the content type of application/javascript
like this:
header("Content-type: application/javascript");
$json = json_encode(
array(
array(
"ProductID" => 1,
"ProductName" => "Chai",
"UnitPrice" => 18,
"UnitsInStock" => 39,
"Discontinued" => false
)
)
);
echo "callback({$json})";
http://us.php.net/manual/en/function.json-encode.php
the example on that page should tell you everything you need to know.