dpr26232 2018-10-08 19:35
浏览 36

php中多个foreach数组的复杂排序

I have 5 different database calls to collect data from users:

$stmt=$db->prepare('SELECT members.email, sites.username, sites.time, sites.id, sites.url, sites.title
                    FROM sites
                    LEFT JOIN members
                    ON members.username=sites.username
                    LEFT JOIN list_friends 
                    ON
                    (CASE
                     WHEN friend1 = :username THEN friend2
                     WHEN friend2 = :username THEN friend1
                    END) = sites.username
                    WHERE sites.username=:username
                    OR (list_friends.friend1 <> :username)
                    OR (list_friends.friend2 <> :username)
                    ORDER BY sites.time ASC');
$stmt->bindParam(':username', $username);
$stmt->execute();
$row2 = $stmt->fetchAll();

Each database call is a unique fetchAll variable: $row, $row1, $row2, etc.

I then create 5 different array sets based on that data.

foreach( $row2 AS $sites_table ) {
    $data[] = array(
        'type' => 'sites',
        'id' => $sites_table["id"],
        'time' => $sites_table["time"],
        'site' => $sites_table["url"],
        'title' => $sites_table["title"],
        'username' => $sites_table["username"],
        'email' => $sites_table["email"]
    );
}

I have a comparing function

function cmp($a, $b)  {
    $ad = new DateTime($a['time']);
    $bd = new DateTime($b['time']);

    if ($ad == $bd) {
        return 0;
    }

    return $ad < $bd ? -1 : 1;
}

And finally I do a sort based on that compare.

if (isset($data) && $data !== NULL) {
    usort($data, "cmp");
    for($i=(count($data)-1)-($feed-1);$i>=(0);$i--){
        if ($i >= 0) {
            // echo out result of arrays
        }
    }
}

My end goal is to have all the data displayed sorted by data['time'] with the latest data['time'] always displayed on top, and then the next latest, and the next so on.

Unfortunately it appears that the sorting is only working per array, per user, instead of all the arrays data combined for all users combined.

so it is displaying output such as

user1 -> $row1 -> 1 minute ago
user2 -> $row5 -> 30 seconds ago
user1 -> $row2 -> 10 seconds ago
user3 -> $row1 -> 4 minutes ago
user1 -> $row1 -> 2 minutes ago
user2 -> $row1 -> 45 seconds ago
user2 -> $row5 -> 55 seconds ago
etc.

So how would I make a sorting algorithm that shows the latest timestamps in order regardless of row or user

user1 -> $row2 -> 10 seconds ago
user2 -> $row5 -> 30 seconds ago
user2 -> $row1 -> 45 seconds ago
user2 -> $row5 -> 55 seconds ago
user1 -> $row1 -> 1 minute ago
user1 -> $row1 -> 2 minutes ago
user3 -> $row1 -> 4 minutes ago
etc.
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效