简体   繁体   English

按降序对列上的数组进行排序

[英]Sort array on column in descending order

I have some of the data from mysql tables and I am trying to calculate speed on the fly and displaying it on graphs.我有一些来自 mysql 表的数据,我正在尝试动态计算速度并将其显示在图表上。 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.我想按降序显示速度列,我尝试了 asort 和 rsort 但 function 对我不起作用。 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.数组看起来像这样:需要按数组索引 [4] 排序,这是动态计算的速度列。

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
                )

        )

)

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:这是一个包含 2 个盘子和 3 个日期的测试用例:

$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) YX11WCY(速度18)
  • YX94RYB (speed 15) YX94RYB(速度15)

The second table shows:第二张表显示:

  • YX11WCY (speed 12) YX11WCY(速度12)
  • YX94RYB (not seen) YX94RYB(未见)

The third table shows:第三张表显示:

  • YX94RYB (not seen) YX94RYB(未见)
  • YX11WCY (not seen) YX11WCY(未见)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM