php将Mysql查询结果合并到一个数组中


php Join Mysql Query Results Into One Array

如果问题不好,首先很抱歉,我的英语不太好。

我有以下查询

SELECT 
    * 
FROM 
    `stream_post` 
        LEFT JOIN `stream_comment` 
            ON (`stream_post`.`stream_id` = `stream_comment`.`comment_stream_id`) 
        JOIN `users_metadata` 
            ON (`stream_post`.`user_id` = `users_metadata`.`user_id`) 
ORDER BY 
    `stream_id` DESC 
LIMIT 10

事实上,问题是我想在同一页上使用这些评论

所以它看起来像这个

parrent post 1
   children comment 1
   children comment 1
parrent post 2
   children2 comment 1
   children2 comment 1

我也将帖子id(流id是什么)保存在评论表中,并加入它们。

但当我得到结果时,ut看起来像这个

Array
(
    [stream_id] => 1
    [user_id] => 1
    [stream_text] => dasdadada
    [date] => 1346400535
    [comment_id] => 1
    [comment_stream_id] => 1
    [comment_user_id] => 1
    [comment_text] => comment one
    [posted] => 0
    [full_name] => Henry Marg
    [gender] => 1
    [location] => Los Angeles
    [birth_year] => 2012
    [birth_month] => 1
    [birth_day] => 1
    [work_location] => Tesco
    [about] => Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
)

Array
(
    [stream_id] => 1
    [user_id] => 1
    [stream_text] => dasdadada
    [date] => 1346400535
    [comment_id] => 1
    [comment_stream_id] => 1
    [comment_user_id] => 1
    [comment_text] => comment 2
    [posted] => 0
    [full_name] => Henry Marg
    [gender] => 1
    [location] => Los Angeles
    [birth_year] => 2012
    [birth_month] => 1
    [birth_day] => 1
    [work_location] => Tesco
    [about] => Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
)

所以问题是,当我转发这些时,我得到的评论是分开的,而不是合并的?

所以我想看起来像这个

rray
(
    [stream_id] => 1
    [user_id] => 1
    [stream_text] => dasdadada
    [date] => 1346400535
    [comment_id] => 1
    [comment_stream_id] => 1
    [comment_user_id] => 1
    [comment_text] => comment one
    [comment_text] => comment two
    [posted] => 0
    [full_name] => Henry Marg
    [gender] => 1
    [location] => Los Angeles
    [birth_year] => 2012
    [birth_month] => 1
    [birth_day] => 1
    [work_location] => Tesco
    [about] => Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
)

有办法做到这一点吗?请有人给我一个提示或例子好吗?

会非常高兴

很抱歉,事情就是这样。除非将所有数据放入数组中,否则可以使用foreach或for语句按顺序遍历comment_text。

在一个数组中不能有两个名称相同的键。您需要在进行时重命名密钥(可能会在末尾添加一个数字)

[comment_text_1] => comment one
[comment_text_2] => comment two

或者你可以把每个元素变成这样的子元素数组——这在代码中更有用

[comment_text] => array('comment one', 'comment two')