我的PHP脚本应该从API接收不确定数量的JSON数据,将其存储在数组中,然后对数组中的每个项运行SQL查询。然后以JSON对象的形式返回查询。
我的API如下所示:
if($_POST(['id'])){
$id= $_POST['id'];
foreach($value as $id){
$sql = "SELECT id, header, body FROM `messages` WHERE id= $id";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $id);
$stmt->execute();
while ($row = mysqli_fetch_assoc($stmt)) {
$response = $row ['id'];
$response = $row ['header'];
$response = $row ['body'];
}
$stmt->close();
}
return json_encode($response);
}
我需要我的JSON看起来像这样
{
"resoponse": {
"check": "check"
},
"code": [{
"id": "id",
"header": "header",
"body": "body"
},
{
"id": "id",
"header": "header",
"body": "body"
},
{
"id": "id",
"header": "header",
"body": "body"
}
]
}
我将JSON返回到一个运行kotin的android应用程序中,如果我遗漏了什么,请告诉我。