Sorting array by date and another by that one in PHP

大憨熊 提交于 2020-01-11 07:57:09

问题


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>";
}

?>

回答1:


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

array_multisort($date, $amount);

To sort descending:

array_multisort($date, SORT_DESC, $amount);



回答2:


$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");

$combinedArray = array_combine($date,$amount);
ksort($combinedArray);

foreach ($combinedArray as $dt => $amt)
   print "$dt\t$amt\n";



来源:https://stackoverflow.com/questions/59652208/sorting-array-by-date-and-another-by-that-one-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!