在PHP中按日期对数组进行排序,并按该数组对另一个数组进行排序 [英] Sorting array by date and another by that one in PHP

查看:383
本文介绍了在PHP中按日期对数组进行排序,并按该数组对另一个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何对这两个数组的日期进行明智的排序,一个数组具有所有日期,请告诉我一种解决此问题的方法 我尝试过的代码

how can I sort these two arrays date wise one array has all the dates please tell me a way to fix this problem The code i tried

<?php
$date=array("2018-09-28","2018-11-26","2018-12-26","2019-01-25","2019-02-25","2019-03-25","2019-04-25","2019-05-27","2019-06-25","2019-07-25","2019-08-26","2019-09-25","2019-10-25","2019-11-25","2019-12-26","2017-05-30","2017-05-31","2017-10-26","2017-10-27","2020-01-04");
$amount=array("-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-1000","-50000","-50000","-70000","-69414","276521");
$count= count($date);
echo "<table>"
echo "<tr>";
echo "<th>DATES</th>";
echo "<th>Amount</th>";
echo "</tr>";

for($i=0;$i<$count;$i++)
{
    echo "<tr>";
    echo "<td>".$date[$i]."</td>";
    echo "<td>".$amount[$i]."</td>";
    echo "</tr>";
}

?>

推荐答案

这将对日期数组进行升序排序,并对金额数组按日期数组进行排序:

This will sort the date array ascending and sort the amount array by the date array:

array_multisort($date, $amount);

以降序排列:

array_multisort($date, SORT_DESC, $amount);

这篇关于在PHP中按日期对数组进行排序,并按该数组对另一个数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆