I would like to format the echo json_encode, the output is currently
{"results":{"course":"CC140","books":{"book":[[{"id":"300862","title":"Building object-oriented software","isbn":"0070431965","borrowedcount":"6"}]]}}}
Whereas i would like to to output like this:
{
"results": {
"course": "CC140",
"books": {
"book": [
[
{
"id": "300862",
"title": "Building object-oriented software",
"isbn": "0070431965",
"borrowedcount": "6"
}
]
]
}
}
}
This is the code that makes the JSON
$temp = array();
foreach ($my_array as $counter => $bc) {
$temp['id'] = "$id[$counter]";
$temp['title'] = "$title[$counter]";
$temp['isbn'] = "$isbn[$counter]";
$temp['borrowedcount'] = "$borrowedcount[$counter]";
$t2[] = $temp;
}
$data = array(
"results" => array(
"course" => "$cc",
"books" => array(
"book" =>
array(
$t2
)
)
)
);
echo json_encode($data);
Any help or pointers would be appreciated, thanks
Adding this
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);
formats the JSON, but the header also outs the entire HTML document