// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM recipe");
$encode = array();
while($allRow = mysqli_fetch_array($result))
{
$new = array(
'id' => $allRow['id']);
$encode[] = $new;
}
echo json_encode($encode);
mysqli_close($con);
?>
我的Json格式不好..
[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"}]
我需要这样的事情 enter link description here
答案 0 :(得分:2)
对于您的每个食谱,您可以添加要显示的属性。
$new = array(
'id' => $allRow['id'],
'title'=>$allRow['title'],
'ingredients'=>$allRows['ingredients']);
$encode[] = $new;
等。
然后,在调用json_encode时,您将在编码对象周围创建一个包装器
$json = json_encode(array("recipes"=>$encode));