我有一個數組,我想在運行 foreach 循環(huán)之前根據它的特定鍵進行排序我已經嘗試過 ksort 但它在這種情況下不起作用[0]=> object(stdClass)#4829 (23) { ["ID"]=> string(4) "4089" ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-10-31 06:28:57" ["post_date_gmt"]=> string(19) "2018-10-31 06:28:57" ["post_content"]=> string(48) "Sessions move 1 hour earlier from 30th September" ["post_title"]=> 'test'}[1]=> object(stdClass)#4830 (24) { ["ID"]=> string(4) "4030" ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2018-10-31 06:28:57" ["post_date_gmt"]=> string(19) "2018-10-31 06:28:57" ["post_content"]=> string(48) "Sessions move 1 hour earlier from 30th September" ["post_title"]=> 'test'}這是我從我的數據庫中獲取的數據,我想以相對于post_date. 我需要在為每個循環(huán)運行之前對其進行排序以顯示課程
2 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
最好的方法是在查詢中按 post_date 排序。
但是,如果你真的想用 PHP 來做,你可以使用array_multisort
并像這樣使用它:
對于PHP 5.6+:
$dates = array_column($yourArray, 'post_date');
array_multisort($dates , SORT_NUMERIC, $yourArray);
如果PHP 版本較低:
$dates=array();
foreach ($data as $key => $row) {
$dates[$key] = $row['post_date'];
}
array_multisort($dates , SORT_NUMERIC, $yourArray);
- 2 回答
- 0 關注
- 137 瀏覽
添加回答
舉報
0/150
提交
取消