Php循环foreach复制

时间:2013-02-05 09:34:33

标签: php arrays loops foreach

我有一个线程表和一个注释表。一个线程可以有很多注释 当我做mysql查询。由于许多评论,它变成了重复的线程。 如何分组tid?

我在php中试过foreach但仍然不知道。

   Array
(
    [0] => Array
        (
            [tid] => 110
            [title] => This is question
            [vid] => 175
            [yesno] => 1
        )

    [1] => Array
        (
            [tid] => 110
            [title] => This is question
            [vid] => 179
            [yesno] => 0
        )
)

我其实想要这个

 Array
(
   [0] => Array
        (
            [tid] => 110
            [title] => This is question

            [0] => Array(
                         [vid] => 175
                         [yesno] => 1
                   )
            [1] => Array(
                         [vid] => 176
                         [yesno] => 0
                   )
        )

)

2 个答案:

答案 0 :(得分:0)

您可以使用类似这样的查询

"SELECT ... FROM tablename GROUP BY thread.tid"
         ^ --- Field name

答案 1 :(得分:0)

$query='select distinct tid,title from thread order by tid';
$thread_id=mysql_query($query);
while($result=mysql_fetch_assoc($thread_id))
{
   $data[]=$result;
   $query1='select vid,yesno from comments where tid='.$result['tid'];
   $comment=mysql_query($query1);
   while($result1=mysql_fetch_assoc($comment))
   {
      $data['comments'][]=$result1;
   }
}
print_r($data);