我有以下数组:
$inboxMessages = array(
105775 => array( //index is thread_id
0 => array(
'id' => 85,
'thread_id' => 105775,
'message' => "hello",
'created_at' => "May 20, 2015",
'datetime' => 1432118191,
'sender_id' => 13,
),
1 => array(
'id' => 70,
'thread_id' => 105775,
'message' => "hii",
'created_at' => "May 19, 2015",
'datetime' => 1432021227,
'sender_id' => 13,
)
),
224199 => array( //index is thread_id
0 => array(
'id' => 88,
'thread_id' => 224199,
'message' => "yessss...",
'created_at' => "May 20, 2015",
'datetime' => 1432306513,
'sender_id' => 14,
),
1 => array( //index is thread_id
'id' => 75,
'thread_id' => 224199,
'message' => "hellowwww...",
'created_at' => "May 19, 2015",
'datetime' => 1432021227,
'sender_id' => 14,
)
),
107917 => array( //index is thread_id
0 => array(
'id' => 56,
'thread_id' => 107917,
'message' => "how r u??",
'created_at' => "May 16, 2015",
'datetime' => 1431792155,
'sender_id' => 14,
),
1 => array(
'id' => 30,
'thread_id' => 107917,
'message' => "hi.. i m fine.",
'created_at' => "May 6, 2015",
'datetime' => 1430920006,
'sender_id' => 14,
),
2 => array(
'id' => 30,
'thread_id' => 107917,
'message' => "hi!!!!..how r u??",
'created_at' => "May 6, 2015",
'datetime' => 1430920006,
'sender_id' => 14,
)
),
378552 => array( //index is thread_id
0 => array(
'id' => 108,
'thread_id' => 378552,
'message' => "hi",
'created_at' => "May 29, 2015",
'datetime' => 1432906923,
'sender_id' => 14,
),
1 => array(
'id' => 107,
'thread_id' => 378552,
'message' => "hi.",
'created_at' => "May 29, 2015",
'datetime' => 1432903194,
'sender_id' => 14,
)
)
);
所以我需要这样的输出:
$inboxMessages = array(
378552 => array( //index is thread_id
0 => array(
'id' => 108,
'thread_id' => 378552,
'message' => "hi",
'created_at' => "May 29, 2015",
'datetime' => 1432906923,
'sender_id' => 14,
),
1 => array(
'id' => 107,
'thread_id' => 378552,
'message' => "hi.",
'created_at' => "May 29, 2015",
'datetime' => 1432903194,
'sender_id' => 14,
)
),
224199 => array( //index is thread_id
0 => array(
'id' => 88,
'thread_id' => 224199,
'message' => "yessss...",
'created_at' => "May 20, 2015",
'datetime' => 1432306513,
'sender_id' => 14,
),
1 => array( //index is thread_id
'id' => 75,
'thread_id' => 224199,
'message' => "hellowwww...",
'created_at' => "May 19, 2015",
'datetime' => 1432021227,
'sender_id' => 14,
)
),
105775 => array( //index is thread_id
0 => array(
'id' => 85,
'thread_id' => 105775,
'message' => "hello",
'created_at' => "May 20, 2015",
'datetime' => 1432118191,
'sender_id' => 13,
),
1 => array(
'id' => 70,
'thread_id' => 105775,
'message' => "hii",
'created_at' => "May 19, 2015",
'datetime' => 1432021227,
'sender_id' => 13,
)
),
107917 => array( //index is thread_id
0 => array(
'id' => 56,
'thread_id' => 107917,
'message' => "how r u??",
'created_at' => "May 16, 2015",
'datetime' => 1431792155,
'sender_id' => 14,
),
1 => array(
'id' => 30,
'thread_id' => 107917,
'message' => "hi.. i m fine.",
'created_at' => "May 6, 2015",
'datetime' => 1430920006,
'sender_id' => 14,
),
2 => array(
'id' => 30,
'thread_id' => 107917,
'message' => "hi!!!!..how r u??",
'created_at' => "May 6, 2015",
'datetime' => 1430920006,
'sender_id' => 14,
)
),
);
我希望按datetime
的{{1}}子数组的值对主数组进行排序。
thread_id
数组已经在排序。
所以我只想按thread_id
的子数组排序主数组。
我需要最近的datetime在数组上排名第一
我到目前为止尝试了它对我有用:
thread_id
并尝试使用消息function comp($a, $b) {
if ($a[0]->datetime == $b[0]->datetime) {
return 0;
}
return $a[0]->datetime > $b[0]->datetime ? -1 : 1;
}
uasort($inboxMessages, "comp");
:
id
答案 0 :(得分:1)
使用uasort
function cmp($a, $b) {
if ($a[0]['datetime'] == $b[0]['datetime']) {
return 0;
}
return ($a[0]['datetime'] < $b[0]['datetime']) ? 1 : -1;
}
uasort($inboxMessages, "cmp");
根据PHP版本,您可以指定&#34; cmp&#34;作为匿名函数。另请注意,此方法依赖于每个线程中至少包含一条消息的事实。
答案 1 :(得分:1)
您可以按如下方式使用uasort()
来实现以下格式
function comp($a, $b) {
if ($a[0]['datetime'] == $b[0]['datetime']) {
return 0;
} return $a[0]['datetime'] > $b[0]['datetime'] ? -1 : 1;
}
uasort($inboxMessages, "comp");