如何以正确的格式返回我的JSON数组?

时间:2017-08-04 23:59:45

标签: php arrays json

当我回显变量$contact_username时,我可以在表单中看到Android logcat中的响应(5个值,这是正确的数量):+11+22+33+44+55

我无法将此作为json数组返回,因此我可以在表单中看到它,

[{"contact_phonenumber":"+11"},{"contact_phonenumber":"+22"},{"contact_phonenumber":"+33"},{"contact_phonenumber":"+44"},{"contact_phonenumber":"+55"}]

如上所述回复$contact_username的我的Php文件就像:

//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();

        ?>

2 个答案:

答案 0 :(得分:1)

$contacts = [];

foreach ($array as $value)
{
    $result = $stmt->get_result(); 

    while ($row = $result->fetch_assoc()) {
        $contacts[]["contact_phonenumber"] = $row['username'];
    }
}

echo json_encode($contacts);

...将产生:[{"contact_phonenumber":"+11"},{"contact_phonenumber":"+22"},{"contact_phonenumber":"+33"},{"contact_phonenumber":"+44"},{"contact_phonenumber":"+55"}]

答案 1 :(得分:0)

而不是:

$contacts = [];
    foreach ($array as $value)
    {
        $result = $stmt->get_result(); 

        while ($row = $result->fetch_assoc()) {
            $contacts[]["contact_phonenumber"] = $row['username'];
        }
    }

应该是:

 $results = array();
    foreach ($array as $value)
    {
        $result = $stmt->get_result(); 

                if(!empty($row['username'])) {
                $results[] = array('contact_phonenumber' => $row['username']);
                        }
    }