我想知道如何修改我的代码(如下)以获取
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
而不是
[
{
"Email":"john@appleseed.com",
"Password":"4321",
"lijst":"eieren, kaas, melk, 2 uien, brood(volkoren)",
"fname":"John",
"lname":"Doe",
"PhoneNumber":"6123456"
}
]
这是产生上述代码的代码:
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM UserInfo";
// Check if there are results
if ($result = mysqli_query($con, $sql)) {
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object()) {
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
// number is if u add ?k to the URL u will only get that output!
if ($number != "") {
echo json_encode($resultArray[$number]);
} else {
echo json_encode($resultArray);
}
}
答案 0 :(得分:1)
所以你想添加一个名为contacts
的密钥?
在编码之前,执行以下操作:
if($number != ""){
echo json_encode(array('contacts' => $resultArray[$number]));
}else {
echo json_encode(array('contacts' =>$resultArray));
}