2017-08-08 39 views
1

當我贊同$json2JSON 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; 

    } 

     ?> 

回答

5

聲明while環這樣的外results陣列:

$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; 
+0

我認爲你是在正確的軌道上,但你一定要'json_encode'和'echo' _inside_ while循環? –

+0

我認爲他只是迴應,以驗證他的輸出,我會發表評論, – yoeunes

+1

完成,謝謝你 – yoeunes

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; 
+0

Tx,試過了。你能看到我更新的問題,從它說'編輯'的地方嗎?按照你的建議,但我仍然無法讓我的JSON正確顯示。 – CHarris

2

在下面的代碼中你需要r調整一些變量以獲得所需的效果:

$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; 
+0

你能看到我更新的問題,從它說'編輯'的地方嗎?按照你的建議,但我無法讓我的JSON正確顯示。 – CHarris

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","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 
$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; 
     } 

      ?> 
相關問題