0

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

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

2 回答 2

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()中,如果存在,尝试将数组与唯一值合并。

希望这可以帮助 ...

于 2013-04-28T11:10:38.023 回答
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
于 2013-04-28T10:03:31.837 回答