0

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

?>
2
  • So you want to keep the amount associated with the same datae after the dort I assume Commented Jan 8, 2020 at 18:59
  • Have you made any efforts to solve this yourself? Commented Jan 8, 2020 at 19:03

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

each amount is correct with the a particular date,means for date 2018-09-28 the amount is -1000
I know that, that's exactly what this does. 3v4l.org/iEOHa
0
$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";

2 Comments

can't do this in other manner like Ammount Date the amount the date
Sure you can. After $date = array_keys($combinedArray); $amount = array_values($combinedArray);. But why not just do it this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.