当我回显变量$contact_username
时,我可以在窗体中看到我的Android logcat中的响应(5个值,这是正确的数量):+11+22+33+44+55
。如何以正确的格式返回我的JSON数组?
我有麻烦返回这是一个JSON数组,所以我可以在表单中看到它,
[{"contact_phonenumber":"+11"},{"contact_phonenumber":"+22"},{"contact_phonenumber":"+33"},{"contact_phonenumber":"+44"},{"contact_phonenumber":"+55"}]
我的PHP文件,如上呼应$contact_username
是这样的:
//stuff here
foreach ($array as $value)
{
// stuff here
$result = $stmt->get_result();
$contact_username = "";
while ($row = $result->fetch_assoc()) {
$contact_username = $row['username'];
}
echo $contact_username;
所以呼应$contact_username;
给我+11+22+33+44+55
我想返回作为JSON数组。
我能得到最接近的是下面的代码,但它给了我:
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][{"contact_phonenumber":"+11"}][][][][][][][][][{"contact_phonenumber":"+22"}][][][] etc... etc...
我怎样才能得到它作为一个JSON数组,如果没有空括号?这里是我的尝试,但它显然不正确:
//stuff here
foreach ($array as $value)
{
// stuff here
$result = $stmt->get_result();
$results = [];
$contact_username = "";
while ($row = $result->fetch_assoc()) {
$contact_username = $row['username'];
array_push($results,['contact_phonenumber' => $contact_username]);
}
$json2 = json_encode($results);
echo $json2;
编辑:我张贴我的PHP文件的全部代码如下
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//***************************************************
require('dbConnect.php');
//this is the user_id in the user table
$Number = $_POST['phonenumberofuser'];
// get the username of the user in the user table, then get the matching user_id in the user table
// so we can check contacts against it
$query = "SELECT * FROM user WHERE username = ?";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param('s', $Number) or die ("MySQLi-stmt binding failed ".$stmt->error);
$stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
//this is the user_id in the user table of the user
$user_id = $row["user_id"];
}
//post all contacts in my phone as a JSON array
$json = $_POST['phonenumberofcontact'];
//decode the JSON
$array = json_decode($json);
//We want to check if contacts in my phone are also users of the app.
//if they are, then we want to put those phone contacts into the contacts table, as friends of user_id , the user of the app
$query = "SELECT * FROM user WHERE username = ?";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param('s', $phonenumberofcontact) or die ("MySQLi-stmt binding failed ".$stmt->error);
$contacts = [];
//for each value of phone_number posted from Android, call it $phonenumberofcontact
foreach ($array as $value)
{
$phonenumberofcontact = $value->phone_number;
$stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
//store the result of contacts from the user's phonebook (that is, the result of the above query, $stmt) that are using the app
$result = $stmt->get_result();
//In this while loop, check the $phonenumberofcontact in the user's phonebook and who are users of the app against
//the user's contacts table. Put the shared contacts in the contacts table for that user.
while ($row = $result->fetch_assoc()) {
$contacts[]["contact_phonenumber"] = $row['username'];
}
echo json_encode($contacts);
}
$stmt->close();
?>
空括号不是由于JSON编码,你的循环是造成他们。也许你应该使用更常见的附加数组来简化代码。 '$ results [] = ['name'=>'value']' – Geoffrey
'json_encode'不可能产生这样的输出。这里的//东西可能是相关的。 – mario
您介意通过输入'print_r($ array);'来向我们展示结果吗? – Spectarion