我想格式化回声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