2017-08-04 7 views
0

私が変数$contact_usernameをエコーすると、私のAndroidのlogcatのフォームに応答が表示されます(正しい値である5つの値):+11+22+33+44+55JSON Arrayを正しい形式で返すにはどうすればよいですか?

私はトラブル私は形でそれを見ることができるようにJSON配列としてこれを返すを抱えている、上記のように$contact_usernameをエコーする

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

私の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; 

は私にJSON配列として返りたい+11+22+33+44+55を返します。

私が得ることができる最も近い、以下のコードであるが、それは私を与える:

[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][{"contact_phonenumber":"+11"}][][][][][][][][][{"contact_phonenumber":"+22"}][][][] etc... etc... 

私はJSON配列として、空の括弧なしでそれを取得できますか?ここに私の試みはあるが、それは明らかに間違っています:

EDIT
//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 

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

以下の私のPHPファイルのコード全体を掲載していますが...生成されます。[{"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 "}" {11 "}" {11 "}" {11 "}" {11 "}" {11 "}" "contact_phonenumber": "+11"} "[contact_phonenumber": "+ 11"}] [{contact_phonenumber ":" 11 " "+11"}、{"conta ct_phonenumber ":" + 22 "}] etc etc ...それが何か助けであれば、質問にすべてのコードを投稿します。 – CHarris

+0

ループが正しく機能していません。私は単純な値を試してみました。 '$ array'のように、残りのコードを見てみましょう。 – Spectarion

関連する問題