为什么我的JSON数组没有以正确的格式返回?

时间:2017-08-08 18:41:54

标签: php while-loop

当我回复$json2时,我的JSON Array会被返回,如:

[{"contact_phonenumber":"12345"}][{"contact_phonenumber":"67890"}][{"contact_phonenumber":"123456"}][{"contact_phonenumber":"78901"}][{"contact_phonenumber":"234567"}][{"contact_phonenumber":"8901234"}]

对我来说它看起来不像JSON Array,我确定我的循环出现了问题 - 也许是因为while嵌套在{ {1}}或者其他东西,但即使我改变它,我也会得到相同的结果。你能帮忙吗?

我希望for each成为:

JSON array

这是我的代码:

[{"contact_phonenumber":"12345"}, {"contact_phonenumber":"67890"},
 {"contact_phonenumber":"123456"}, {"contact_phonenumber":"78901"},{"contact_phonenumber":"234567"}, {"contact_phonenumber":"8901234"}]

编辑:非常感谢您的帮助,但现在当我按照您的大部分答案提出建议时 - 宣布<?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","8901234"]'; $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

你知道如何在没有空括号的情况下得到匹配的数字吗?而且,在开头和结尾只应该是方括号 - 就像JSON数组一样。

这是我的更新代码:

[][][{"contact_phonenumber":"12345"}][{"contact_phonenumber":"67890"}][{"contact_phonenumber":"123456"}][{"contact_phonenumber":"78901"}][{"contact_phonenumber":"234567"}][{"contact_phonenumber":"8901234"}]

4 个答案:

答案 0 :(得分:5)

results循环之外声明while数组,如下所示:

$results = array();
foreach ($array as $value)
{
    $phonenumberofcontact = $value;

    $stmt2->execute() or die ("MySQLi-stmt execute failed ".$stmt2->error);

    $result2 = $stmt2->get_result();

    while ($row = $result2->fetch_assoc()) {
        if(!empty($row['username'])) {
            $results[] = array('contact_phonenumber' => $row['username']);//remove extra ,
        }
    }
}

$json2 = json_encode($results);
echo $json2;

答案 1 :(得分:2)

在代码的以下部分中,您需要重新排列一些变量以获得所需的效果:

$results = array();  // Move this outside the loop

while ($row = $result2->fetch_assoc()) {
    $results[] = array("contact_phonenumber" => $row["username"]); 
}

// As well as these two line
$json2 = json_encode($results); 
echo $json2;

答案 2 :(得分:1)

可能你可以改变你的阻止这样的东西:

$results = array();

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(
         'contact_phonenumber' => $row['username'], 
         );
    }

    $json2 = json_encode($results); 
    echo $json2;

答案 3 :(得分:0)

因为你在foreach循环中回显你的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","8901234"]';
    $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
$i = 0;
$tempArray = array();
        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'], 
             );


               $tempArray[$i] = $results;
    $i++;
        }
$json2 = json_encode($results); 
echo $json2;
        }

            ?>