我有PHP代碼,它從mysql數據庫的表中獲取數據並以JSON格式顯示該數據。如何更改JSON輸出的格式?
這是我的代碼:
<?php
include ("config/config.php");
//build query
$query =
"SELECT
ProductID,
Name,
Price,
Type,
cat
FROM store ORDER BY ProductID ASC";
$rsPackages = mysqli_query($mysqli_conn,$query);
$arRows = array();
while ($row_rsPackages = mysqli_fetch_assoc($rsPackages)) {
array_push($arRows, $row_rsPackages);
}
header('Content-type: application/json');
echo json_encode($arRows);
?>
從這個代碼的輸出是這樣的:
[{"ProductID":"1","Name":"HTML:Beginners guide","Price":"7.85","Type":"Book","cat":"HTML"},{"ProductID":"4","Name":"HTML: Intermediate","Price":"8.99","Type":"Book","cat":"HTML"},{"ProductID":"5","Name":"HTML: Advanced","Price":"10.99","Type":"Book","cat":"HTML"},{"ProductID":"7","Name":"CSS: Beginners Guide","Price":"7.99","Type":"Book","cat":"CSS"},{"ProductID":"8","Name":"CSS: Intermediate","Price":"8.99","Type":"Book","cat":"CSS"},{"ProductID":"9","Name":"CSS: Advanced","Price":"10.99","Type":"Book","cat":"CSS"},{"ProductID":"10","Name":"PHP: Beginners Guide","Price":"7.99","Type":"Book","cat":"PHP"},{"ProductID":"11","Name":"PHP: Intermediate","Price":"8.99","Type":"Book","cat":"PHP"},{"ProductID":"12","Name":"PHP: Advanced","Price":"10.99","Type":"Book","cat":"PHP"},{"ProductID":"13","Name":"MYSQL- Easy steps","Price":"11.99","Type":"Book","cat":"MYSQL"},{"ProductID":"14","Name":"HTML- Video Guide","Price":"19.99","Type":"CD","cat":"HTML"},{"ProductID":"15","Name":"CSS: Video Guide","Price":"19.99","Type":"CD","cat":"CSS"},{"ProductID":"16","Name":"PHP: Video Guide","Price":"19.99","Type":"CD","cat":"PHP"},{"ProductID":"22","Name":"css book","Price":"3.49","Type":"","cat":""},{"ProductID":"26","Name":"bdkjhedsjbdsasa","Price":"3.59","Type":"","cat":""}]
不過,我想輸出結果出現在這樣的格式:「{」記錄 「:[結果]}」
例如:
{"records":[{"ProductID":"1","Name":"HTML:Beginners guide","Price":"7.85","Type":"Book","cat":"HTML"},{"ProductID":"4","Name":"HTML: Intermediate","Price":"8.99","Type":"Book","cat":"HTML"},{"ProductID":"5","Name":"HTML: Advanced","Price":"10.99","Type":"Book","cat":"HTML"},{"ProductID":"7","Name":"CSS: Beginners Guide","Price":"7.99","Type":"Book","cat":"CSS"},{"ProductID":"8","Name":"CSS: Intermediate","Price":"8.99","Type":"Book","cat":"CSS"},{"ProductID":"9","Name":"CSS: Advanced","Price":"10.99","Type":"Book","cat":"CSS"},{"ProductID":"10","Name":"PHP: Beginners Guide","Price":"7.99","Type":"Book","cat":"PHP"},{"ProductID":"11","Name":"PHP: Intermediate","Price":"8.99","Type":"Book","cat":"PHP"},{"ProductID":"12","Name":"PHP: Advanced","Price":"10.99","Type":"Book","cat":"PHP"},{"ProductID":"13","Name":"MYSQL- Easy steps","Price":"11.99","Type":"Book","cat":"MYSQL"},{"ProductID":"14","Name":"HTML- Video Guide","Price":"19.99","Type":"CD","cat":"HTML"},{"ProductID":"15","Name":"CSS: Video Guide","Price":"19.99","Type":"CD","cat":"CSS"},{"ProductID":"16","Name":"PHP: Video Guide","Price":"19.99","Type":"CD","cat":"PHP"},{"ProductID":"22","Name":"css book","Price":"3.49","Type":"","cat":""},{"ProductID":"26","Name":"bdkjhedsjbdsasa","Price":"3.59","Type":"","cat":""}]}
我應該怎樣改變我的php代碼以獲得上述輸出?
非常感謝您的回答取代你的代碼的最後一行。有效。 – user5679217
@ user5679217不客氣! ':)' –