0

I have some of the data from mysql tables and I am trying to calculate speed on the fly and displaying it on graphs. I would like to display the speed column in descending order, I tried asort and rsort but the function did not work out for me. Here is the code where i am got stuck. I have already calculated the speed and brought it into the array and now need to sort the array in descending order on speed column.

       $myArray[$plate][$inDate] = array($inDateTime, $outDateTime, $inDate, $dur_string,(round($speed,2)));
      // asort($myArray,$speed);--- > this is where i am  stuck.
    }
}
$new_platelist = array_unique($new_platelist);
while ($startLoopDate < $endLoopDate) {
    $dayString = strftime("%a, %d %B %Y", $startLoopDate);
   $dayCheck = strftime("%Y-%m-%d", $startLoopDate);
    print "<h2>$dayString</h2>";
    print "<table width=\"100%\">";
    print " <tr>";
    print " <th>plate</th>";
    print " <th>Entry Time</th>";
    print " <th>Exit Time</th>";
    print " <th>Duration</th>";
    print " <th>Speed MPH</th>";
    print " </tr>";
    foreach($new_platelist as $wlPlate) {
       if (isset($myArray[$wlPlate][$dayCheck])){
           print "<tr>";
           print "<td>$wlPlate</td>";
           if (isset($myArray[$wlPlate][$dayCheck])){
               print "<td>".$myArray[$wlPlate][$dayCheck][0]."</td>";
               print "<td>".$myArray[$wlPlate][$dayCheck][1]."</td>";
               print "<td>".$myArray[$wlPlate][$dayCheck][3]."</td>";
               print "<td>".$myArray[$wlPlate][$dayCheck][4]."</td>";
            }
            else {
                print "<td>Vehicle Not Seen</td>";
                print "<td>Vehicle Not Seen</td>";
                print "<td>Vehicle Not Seen</td>";
            }
        }
    print "</tr>";
    }
    print "</table>";

Array looks like this: Needs sort by array index [4] which is the speed column calculated on fly.

Array
(
    [YX86RYB] => Array
        (
            [2021-09-06] => Array
                (
                    [0] => 2021-09-06 13:55:12
                    [1] => 2021-09-06 13:55:26
                    [2] => 2021-09-06
                    [3] => 14 secs 
                    [4] => 15.66
                )

        )

)

Array
(
    [YX94RYB] => Array
        (
            [2021-09-06] => Array
                (
                    [0] => 2021-09-06 13:55:12
                    [1] => 2021-09-06 13:55:26
                    [2] => 2021-09-06
                    [3] => 14 secs 
                    [4] => 15.66
                )

        )

    [YX11WCY] => Array
        (
            [2021-09-07] => Array
                (
                    [0] => 2021-09-07 09:24:17
                    [1] => 2021-09-07 09:24:32
                    [2] => 2021-09-07
                    [3] => 15 secs 
                    [4] => 14.61
                )

        )

)
Olivier
  • 13,283
  • 1
  • 8
  • 24
preethi
  • 885
  • 5
  • 20
  • 39
  • 1
    Second parameter of `asort()` should not be input from you but just flags. See the docs: https://php.net/asort – ArSeN Sep 27 '21 at 11:05
  • 1
    Does this answer your question? [How to Sort a Multi-dimensional Array by Value](https://stackoverflow.com/questions/2699086/how-to-sort-a-multi-dimensional-array-by-value) – KhorneHoly Sep 27 '21 at 11:07
  • I tried it , but here flags are index number of the array right? so confusing. – preethi Sep 27 '21 at 11:11
  • i dont have a comparison function so cant use usort – preethi Sep 27 '21 at 11:20
  • Of course you don't just "have" it, out of the blue. _Writing_ the comparison function, is _your_ job. – CBroe Oct 05 '21 at 14:17
  • Thanks Cbroe, I know I dont have it and I am not able to understand how to compare two arrays inside an array , I tried to write a function and compare them but was confused which arrays to be compared, can you please shed some light on it – preethi Oct 05 '21 at 15:54

1 Answers1

1

The way you have structured the array is not the best, you should put the date first, then the plate:

$myArray[$inDate][$plate] = array($inDateTime, $outDateTime, $inDate, $dur_string, (round($speed,2)));

Now it becomes easier to sort the plates for a given date.

Here is a way to print the table for a date:

function printTable($myArray, $date, $platelist)
{
    print "<h2>$date</h2>\n";

    print "<table width='100%'>";
    print "<tr>";
    print "<th>plate</th>";
    print "<th>Entry Time</th>";
    print "<th>Exit Time</th>";
    print "<th>Duration</th>";
    print "<th>Speed MPH</th>";
    print "</tr>";

    if(isset($myArray[$date]))
    {
        // Get the cars seen that day
        $cars = $myArray[$date];
        // Sort them by descending speed
        uasort($cars, function($x, $y)
        {
            return $y[4] - $x[4];
        });
        // Print them
        foreach($cars as $plate => $car)
            printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $plate, $car[0], $car[1], $car[3], $car[4]);
    }
    else
        $cars = [];

    // Add the missing vehicles
    $notSeen = 'Vehicle Not Seen';
    foreach($platelist as $plate)
    {
        if(!isset($cars[$plate]))
            printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $plate, $notSeen, $notSeen, $notSeen, $notSeen);
    }

    print "</table>";
}

And here is a test case with 2 plates and 3 dates:

$platelist = ['YX94RYB', 'YX11WCY'];

$myArray['2021-09-06']['YX94RYB'] = ['2021-09-06 13:55:12', '2021-09-06 13:55:26', '2021-09-06', '14 secs', 15.00];
$myArray['2021-09-06']['YX11WCY'] = ['2021-09-06 13:55:12', '2021-09-06 13:55:26', '2021-09-06', '14 secs', 18.00];
$myArray['2021-09-07']['YX11WCY'] = ['2021-09-07 13:55:12', '2021-09-07 13:55:26', '2021-09-06', '14 secs', 12.00];

printTable($myArray, '2021-09-06', $platelist);
printTable($myArray, '2021-09-07', $platelist);
printTable($myArray, '2021-09-08', $platelist);

The first table shows:

  • YX11WCY (speed 18)
  • YX94RYB (speed 15)

The second table shows:

  • YX11WCY (speed 12)
  • YX94RYB (not seen)

The third table shows:

  • YX94RYB (not seen)
  • YX11WCY (not seen)
Olivier
  • 13,283
  • 1
  • 8
  • 24