当我赞同$json2
我JSON Array
被返回,如:为什么我的JSON数组没有以正确的格式返回?
[{"contact_phonenumber":"12345"}][{"contact_phonenumber":"67890"}][{"contact_phonenumber":"123456"}][{"contact_phonenumber":"78901"}][{"contact_phonenumber":"234567"}][{"contact_phonenumber":"89"}]
它看起来并不像一个JSON Array
给我,我敢肯定有什么毛病我的循环 - 也许是因为while
嵌套在for each
什么的,但即使我改变它,我仍然得到相同的结果。你能帮我吗?
我希望我的JSON array
是这样的:
[{"contact_phonenumber":"12345"}, {"contact_phonenumber":"67890"},
{"contact_phonenumber":"123456"}, {"contact_phonenumber":"78901"},{"contact_phonenumber":"234567"}, {"contact_phonenumber":"89"}]
这里是我的代码:
<?php
require('dbConnect.php');
//this is the username in the user table
$Number = "+353872934480";
// 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 corresponding user_id in the user table of the user
$user_id = $row["user_id"];
}
//post all contacts for user_id as a JSON array
$phonenumberofcontact = '["+11111","+222222","12345","67890","123456","78901","234567","89"]';
$array = json_decode($phonenumberofcontact);
//We want to check if contacts of user_id are also users of the app.
$query = "SELECT * FROM user WHERE username = ?";
$stmt2 = $con->prepare($query) or die(mysqli_error($con));
$stmt2->bind_param('s', $phonenumberofcontact) or die("MySQLi-stmt binding failed " . $stmt->error);
//for each value, call it $phonenumberofcontact
foreach($array as $value) {
$phonenumberofcontact = $value;
$stmt2->execute() or die("MySQLi-stmt execute failed " . $stmt2->error);
//match the phone numbers in the JSON Array against those in the user table
$result2 = $stmt2->get_result();
while($row = $result2->fetch_assoc()) {
//make an array called $results
//this is a matching number in user table and in the JSON Array
//call this username contact_phonenumber
$results = array();
$results[] = array(
'contact_phonenumber' => $row['username']
);
$json2 = json_encode($results);
echo $json2;
}
}
编辑:非常感激您的帮助,但现在当我这样做,因为大多数你的答案建议 - 宣布$results
并呼应json
外部while
循环,我得到以下内容:
[][][{"contact_phonenumber":"12345"}][{"contact_phonenumber":"67890"}][{"contact_phonenumber":"123456"}][{"contact_phonenumber":"78901"}][{"contact_phonenumber":"234567"}][{"contact_phonenumber":"89"}]
你知道我怎样才能得到匹配的数字,没有空括号?而且,在开始和结束时只应该是方括号 - 就像JSON数组一样。
这里是我更新的代码:
<?php
require('dbConnect.php');
//this is the username in the user table
$Number = "+353872934480";
// 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 corresponding user_id in the user table of the user
$user_id = $row["user_id"];
}
//post all contacts for user_id as a JSON array
$phonenumberofcontact ='["+11111","+222222","12345","67890","123456","78901","234567","89"]';
$array = json_decode($phonenumberofcontact);
//We want to check if contacts of user_id are also users of the app.
$query = "SELECT * FROM user WHERE username = ?";
$stmt2 = $con->prepare($query) or die(mysqli_error($con));
$stmt2->bind_param('s', $phonenumberofcontact) or die ("MySQLi-stmt binding failed ".$stmt->error);
//for each value, call it $phonenumberofcontact
foreach ($array as $value)
{
$phonenumberofcontact = $value;
$stmt2->execute() or die ("MySQLi-stmt execute failed ".$stmt2->error);
//match the phone numbers in the JSON Array against those in the user table
$result2 = $stmt2->get_result();
//make an array called $results
//this is a matching number in user table and in the JSON Array
//call this username contact_phonenumber
$results = array();
while ($row = $result2->fetch_assoc()) {
$results[] = array('contact_phonenumber' => $row['username']);//remove extra ,
}
$json2 = json_encode($results);
echo $json2;
}
?>
我认为你是在正确的轨道上,但你一定要'json_encode'和'echo' _inside_ while循环? –
我认为他只是回应,以验证他的输出,我会发表评论, – yoeunes
完成,谢谢你 – yoeunes