2013-04-28 120 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