Sorted multidimentional数组一次显示一个数组的值

时间:2018-02-17 06:37:06

标签: php mysql arrays codeigniter codeigniter-3

我有一个由两个合并数组组成的数组,我使用array_multisort()对其进行了排序。问题是它似乎是一次显示来自一个数组的数据,当我需要它来显示数据时,取决于来自两个独立键的高到低值。

现在它看起来像这样......

[ Topic posted 10 minutes ago ]
[ Topic posted 20 minutes ago ]
[ Topic posted 30 minutes ago ]
[ Reply by user, 10 minutes ago ]
[ Reply by user,  20 minutes ago ]
[ Reply by user,  30 minutes ago ]

实际上我需要按此顺序显示

[ Topic posted 10 minutes ago ]
[ Reply by user, 10 minutes ago ]
[ Topic posted 20 minutes ago ]
[ Reply by user, 20 minutes ago ]
[ Topic posted 30 minutes ago ]
[ Reply by user, 30 minutes ago ]

如何实现这一目标?

这是我的PHP代码:

$topics = $this->db->select("*")
    ->order_by("id", "desc")
    ->limit(10)
    ->get("forum_topics")
    ->result();

$replies = $this->db->select("*")
    ->order_by("id", "desc")
    ->limit(10)
    ->get("forum_topics_replies")
    ->result();

$merged = array_merge($topics, $replies);

$sort = array();
foreach($merged as $k => $v) {
    $sort['reply_date'][$k] = $v;
    $sort['topic_date_made'][$k] = $v;
}

array_multisort($sort['topic_date_made'], SORT_DESC, $sort['reply_date'], SORT_DESC, $merged);

return $merged;

修改 如果使用连接查询,该如何实现?

$topics = $this->db->select("*")
 ->from("forum_topics")
 ->limit($limit)
 ->join('forum_topics_replies', 'forum_topics.id = forum_topics_replies.id')
 ->order_by("forum_topics.id", "desc")
 ->order_by("forum_topics_replies.id", "desc")
 ->get()
 ->result();

编辑2: 我需要能够使用以下字段:

主题表:topic_nameidparenttopic_date_made

回复表:parentauthorreply_dateid

2 个答案:

答案 0 :(得分:1)

您可以尝试添加 forum_topic_id id forum_topics之间的公共表列/字段 forum_topics_replies 表。

$sort = array();
foreach($merged as $k => $v) {
    $sort['id'][$k] = $v;// OR $sort['forum_topic_id'][$k] = $v;
    $sort['reply_date'][$k] = $v;
    $sort['topic_date_made'][$k] = $v;
}

array_multisort($sort['id'], SORT_DESC, $sort['topic_date_made'], SORT_DESC, $sort['reply_date'], SORT_DESC, $merged);
  

更新1:

为使arr​​ay_multisort正常工作,每个数组的数组键必须相同。看起来好像是因为数据来自不同的表,而你只是将它们合并而不是它的工作。

所以要么你可以尝试内连接,要加入这两个表,这将为$ merged生成完全相同数量的数组键,或者你可以手动按下两个结果数组来获得所需的$ merged数组。

  

更新2:

$topics = $this->db->select("forum_topics.id as topic_id, *")
 ->from("forum_topics")
 ->limit($limit)
 ->join('forum_topics_replies', 'forum_topics.id = forum_topics_replies.id')
 ->order_by("forum_topics.id", "desc")
 ->order_by("forum_topics_replies.id", "desc")
 ->get()
 ->result();

    $sort = array();
    foreach($topics as $k => $v) {
        $sort['topic_id'][$k] = $v;
        $sort['reply_date'][$k] = $v;
        $sort['topic_date_made'][$k] = $v;
    }

    array_multisort($sort['topic_id'], SORT_DESC, $sort['topic_date_made'], SORT_DESC, $sort['reply_date'], SORT_DESC, $topics);

请尝试以上操作。

答案 1 :(得分:1)

我认为工会声明可以解决你的问题 - 这个怎么样?

$query = $this->db->query('
    SELECT id, 1 as is_topic, parent, "" as author, topic_name, "Topic posted" AS praefix, topic_made_date as union_date FROM forum_topics
    UNION
    SELECT id, 0 as is_topic, parent, author, "" as topic_name, "Reply by user" AS praefix, reply_date as union_date FROM forum_topics_replies
    ORDER BY union_date DESC
    LIMIT 0,10
');