2013-04-28 66 views
0

我的問題是,我有一個SQL查詢填充數組,並希望將數組合併到一個新的數組中,其中查詢的ID不是重複的,我試過了merge_array和當我通過URL執行函數時,它不顯示數組中的內容我只是得到ARRAY.Have也試圖將數組連接到另一個數組並獲得相同的結果,再次我的問題是如何加入數組並正確顯示。PHP將數組連接到數組上sql查詢的結果

$rows[0] = array("181", "a","g"); //Results from previous query 
$rows[1] = array("181","j","L") 
$rows[2] = array("181","p"); 
$rows[3] = array("182","k"); 
$rows[4] = array("183","l"); 
$rows[5] = array("183","p"); 
$id =0; 
$commentsH = ""; 

    while($row=mysql_fetch_array($query_comments)){ 

      If($id == $image){ //image id is the first element in array. 

      $comments[] =$row; 

     $commentsH = $comments.",".$commentsH[$i]; 

      } 

      else{ 

      $id = $image; 
      $i = $i +1; 

      } 

     } 


    $result = array(); 
    $result["result"] = 500; 
    $result["message"] = "Database Read Successfully"; 
    $result["comments"] =$commentsH; 
    echo json_encode($result); 
    exit; 


EXPECTED OUTPUT 

$commentsH[0] = array("181", "a","g","j","L","p"); 
$commentsH[1] = array("182","k"); 
$commentsH[2] = array("183","l","p"); 
+0

那麼...聲明'$ image'在哪裏?您在每次迭代和代碼中覆蓋'$ commentSH','$ commentsH'不是您的_expected output_中顯示的數組,'$ commentsH = $ comments。「,」。$ commentsH [$我];'沒有任何意義.. – dbf 2013-04-28 10:02:35

+0

..有什麼你需要知道關於使用[mysql函數](http://php.net/manual/en/function.mysql-query.php ) – dbf 2013-04-28 10:07:51

+0

$ image是一個查詢的結果,我沒有在上面認爲這很明顯,$ commentsH = $ comments。「,」。comments [$ i]在兩個數組之間用「,」連接數組如果陳述是真的,在其他語言C和Java中很常見。 – Freddy 2013-04-28 10:13:32

回答

0

這段代碼是錯誤的

while($row=mysql_fetch_array($query_comments)){ 

     If($id == $image){ //image id is the first element in array. 

     $comments[] =$row; 

    $commentsH = $comments.",".$commentsH[$i]; 

     } 

     else{ 

     $id = $image; 
     $i = $i +1; 

     } 

    } 

你試圖用錯誤的變量比較,$形象將永遠不會有數組的第一個元素。試試這個

while($row=mysql_fetch_array($query_comments)){ 
     $image = $row[0]; 
     If($id == $image){ //image id is the first element in array. 

    // put your further logic here 
+0

你是正確的,這將是我的下一個任務,但我只是想得到正確的輸出,然後解決這個問題 – Freddy 2013-04-28 10:15:07

0

如上面簡要提到的,你很可能重新工作,您的查詢來獲得一次所有數據,但是,我不知道到底是什麼這個程序是應該做的,或者結構它也不是頁面,所以我會根據你的要求給出一個例子。請記住,比較數組來合併它們應該比我在這裏更加動態,但是這可能是你要添加的東西,因爲我不知道你是如何得到你的數組的。這只是衆多方法來比較和合並一個:

$commentsH = array(); 
$rows = array(); 
$rows[0] = array("181", "a","g"); //Results from previous query 
$rows[1] = array("181","j","L", "z", "a"); 
$rows[2] = array("183", "d", "W", "t"); 
$rows[3] = array("183", "h", "W", "e"); 


// check 2 arrays 
if( $rows[0][0] == $rows[1][0] ){ 
    for($j=1; $j<count($rows[1]); $j++){ 
     $found = false; 
     $val = ""; 
     for($i=1; $i<count($rows[0]); $i++){ 
      if($rows[1][$j] == $rows[0][$i]){ 
       $found = true; 
       break; 
      } 
     } 

     if($found == false) array_push($rows[0], $rows[1][$j]); 
    } 
} 
$commentsH[] = $rows[0]; 

// check two more ... 
if( $rows[2][0] == $rows[3][0] ){ 
    for($j=1; $j<count($rows[3]); $j++){ 
     $found = false; 
     $val = ""; 
     for($i=1; $i<count($rows[2]); $i++){ 
      if($rows[3][$j] == $rows[2][$i]){ 
       $found = true; 
       break; 
      } 
     } 

     if($found == false) array_push($rows[2], $rows[3][$j]); 
    } 
} 
$commentsH[] = $rows[2]; 



// this block simple iterates through the commentsH array and prints out the keys and values cleanly for plain viewing 
for($i=0; $i<count($commentsH); $i++){ 
    foreach($commentsH[$i] as $key=>$val){ 
     echo $key . " => " . $val . "<br />"; 
    } 

    echo "<br /><br />"; 
} 

此外,需要對值進行比較併合並這樣的時候,它往往可以更容易,更合理的使用關聯數組,因爲它們需要唯一的密鑰。你可以重新寫這個來測試一個數組鍵是否已經存在array_key_exists(),如果是這樣,嘗試合併具有唯一值的數組。

希望這會有所幫助...

+0

謝謝你的迴應,將放棄它,並回復給你,謝謝 – Freddy 2013-04-28 18:49:49