Sorting array by date and another by that one in P

2020-04-02 06:46发布

问题:

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