php将数组连接到数组上的sql查询结果

时间:2013-04-28 09:54:46

标签: php

我的问题是,我有一个从SQL查询填充的数组,并希望将数组合并到一个新的数组中,其中查询的id不重复,我尝试了merge_array,当我通过URL执行函数时它没有显示数组中的内容我只是得到ARRAY.Have也尝试将数组连接到另一个数组并获得相同的结果,我的问题是我如何加入数组并正确显示它。

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

2 个答案:

答案 0 :(得分: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;

       }

  }  

你试图与错误的变量进行比较,$ image将永远不会有数组的第一个元素。试试这个

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

答案 1 :(得分: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()的数组键,如果是,请尝试将数组与唯一值合并。

希望这会有所帮助......