0

I have an array in the below format:

Array
(
    [123451000] => Array
        (
            [total] => 70
            [total_tech] => 36
            [duration] => 300
        )

    [123451001] => Array
        (
            [total] => 9
            [total_tech] => 3
            [duration] => 197
        )

    [123451002] => Array
        (
            [total] => 103
            [total_tech] => 97
            [duration] => 273
        )
)

I am looping through it, but wondered if it is possible to order by the total?

EDIT: My current foreach loop

foreach($agents as $agent => $option) {
    //$talktime = secondsToTime($x["talktime_sum"]);
    $display.= '<tr>
    <td>'.get_dtypes('phone', $agent).'</td>
    <td>'.number_format($option["total_calls"]).'</td>
    <td>'.number_format($option["technical_calls"]).'</td>
    <td>'.number_format($option["sales_calls"]).'</td>
    <td>'.number_format($option["other_calls"]).'</td>
    <td>'.$option["total_duration"].'</td>
    </tr>';
}
charlie
  • 415
  • 4
  • 35
  • 83

1 Answers1

1
uasort($array, function ($a, $b) {
    return $a['total'] <=> $b['total'];
});

usort is a function that sorts an array with quick sort, using a user given function for the comparison.

~~uksort is a variant of it, that keeps the array's keys~~

<=> is the "spaceship" operator, it returns -1 if the left side is bigger, 1 if the rigth side is bigger, and 0 if they're equal.

Edit The uksort sorts using the key, not keeping the key. uasort is the one that sorts with the values and also keeps the array keys

Hunman
  • 155
  • 6