我想格式化回聲json_encode,輸出是目前格式回聲json_encode
{"results":{"course":"CC140","books":{"book":[[{"id":"300862","title":"Building object-oriented software","isbn":"0070431965","borrowedcount":"6"}]]}}}
而我想輸出是這樣的:
{
"results": {
"course": "CC140",
"books": {
"book": [
[
{
"id": "300862",
"title": "Building object-oriented software",
"isbn": "0070431965",
"borrowedcount": "6"
}
]
]
}
}
}
這是使代碼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);
任何幫助或指針將不勝感激,謝謝
添加此
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);
格式化JSON,但頭部還奏整個HTML文檔
你怎麼可能會打擾JSON是什麼樣子?它的結構很重要,而不是格式化的方式...... – 2013-02-25 12:29:59
可能添加json頭文件將有所幫助。 – 2013-02-25 12:31:10