按php数组排序

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了按php数组排序脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要通过getNotifications函数中的值’start_date’DESC顺序对通知数组进行排序:

$posts_id_arr = getPoststIds($conn);

foreach ($posts_id_arr as $key => $val) {
  $total_arr[$key] = [
    'notification' => getNotifications($val['post_id'],$user_id,$conn)
  ];
}

$response_array = array('data' => $total_arr,'more things' => $more_array);
echo json_encode($response_array);

现在由于foreach循环,订单是post id.

data {
       notification: 
      [
       {
        post_id: “1",start_date: "2016-10-10 08:00:00",},{
        post_id: “1",start_date: "2016-10-10 12:00:00",}
    ],notification:
      [
        post_id: “2",start_date: "2016-10-10 09:00:00",{
        post_id: “2",start_date: "2016-10-10 13:00:00",}
    ]
}

我需要它是1:08:00,2:09:00,1:12:00,2:13:00

解决方法

但是,您不需要在foreach中执行排序.

您可以尝试以下代码.相应地更改变名称.

foreach ($points as $key => $val) {
    $time[$key] = $val[0];
}

array_multisort($time,SORT_ASC,$points);

这是因为array_multisort的工作方式.它对多个数组进行排序,当$time数组排序时,$points数组根据$time中的数组索引重新排序.但是,array_multisort应该在foreach之后希望这会有所帮助.

@H_502_40@

脚本宝典总结

以上是脚本宝典为你收集整理的按php数组排序全部内容,希望文章能够帮你解决按php数组排序所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。