2017-08-04 66 views
0

当我回显变量$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(); 

     ?> 
+0

空括号不是由于JSON编码,你的循环是造成他们。也许你应该使用更常见的附加数组来简化代码。 '$ results [] = ['name'=>'value']' – Geoffrey

+0

'json_encode'不可能产生这样的输出。这里的//东西可能是相关的。 – mario

+0

您介意通过输入'print_r($ array);'来向我们展示结果吗? – Spectarion

回答

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']); 
         } 
    } 
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"}]

+0

谢谢,但我现在得到的是:[] [ ] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [ ] [] [] [] [] [] [] [{ “contact_phonenumber”: “+ 11”}] [{ “contact_phonenumber”: “+ 11”}] [{ “contact_phonenumber”: “+ 11”}] [ { “contact_phonenumber”: “+ 11”}] [{ “contact_phonenumber”: “+ 11”}] [{ “contact_phonenumber”: “+ 11”}] [{ “contact_phonenumber”: “+ 11”}] [{” contact_phonenumber “:” + 11 “}] [{” contact_phonenumber “:” + 11 “}] [{” contact_phonenumber “:” + 11 “},{” contact_phonenumber “:” + 22 “}] [{” contact_phonenumber“: “11”},{“CONTA ct_phonenumber“:”+ 22“}]等等等等......如果有任何帮助,我会在问题中发布我的所有代码。 – CHarris

+0

然后你的循环工作不正常。我尝试了简单的值,它工作。让我们看看其余的代码,例如'$ array'。 – Spectarion