2

I am attempting to sort an array based off the key, "dispatched". However, it is not working. Does anyone have any pointers to get this code working? Thanks:

Array:

Array
(
    [0] => Array
        (
            [wcccanumber] => 130700203
            [call] => SEIZURES
            [address] => 221 S PINE ST
            [county] => C
            [station] => CNB
            [department] => CANBY FIRE DISTRICT #62
            [units] => E61, M62
            [dispatched] => 20:43:59
        )

    [1] => Array
        (
            [wcccanumber] => 130700198
            [call] => CARD/RESP ARREST
            [address] => 40781 HWY 26
            [county] => C
            [station] => SAN
            [department] => SANDY FIRE DISTRICT #72
            [units] => 3709, CH37, M1, M272, R71
            [dispatched] => 19:33:27
        )

    [2] => Array
        (
            [wcccanumber] => 130700337
            [call] => TRAUMA C1
            [address] => 16500 SW CENTURY DR
            [county] => W
            [station] => SHW
            [department] => TUALATIN VALLEY FIRE & RESCUE
            [units] => E33, METWA, MW68
            [dispatched] => 21:40:13
        )

    [3] => Array
        (
            [wcccanumber] => 130700335
            [call] => FALL C1
            [address] => 48437 NW PONGRATZ RD
            [county] => W
            [station] => BUX
            [department] => BANKS FIRE DISTRICT #13
            [units] => E14, METWA, MW57, R13
            [dispatched] => 21:07:48
        )

)

Code:

public function sortActiveCalls () 
{
    foreach ($this->getActiveCalls() as $key => $val) {
           $time[$key] = $val['dispatched'];
    }

    array_multisort($time, SORT_ASC, $this->getActiveCalls());
}
0

2 Answers 2

4

I find it easier just to use usort

 usort($array, function ($a, $b) {
       return strtotime($a["dispatched"]) - strtotime($b["dispatched"]);
 });

For your case you would reimplement your sortActiveCalls method as

public function sortActiveCalls(){
     $data = $this->getActiveCalls();
     usort($data, function ($a, $b) {
       return strtotime($a["dispatched"]) - strtotime($b["dispatched"]);
     });
     return $data;
}

Not this will only work in php 5.3 and up

If you are using an older version of php you will have to define a separate function to do the sort like

if (!function_exists("sortByDispatched")){
  function sortByDispatched($a, $b){
    return strtotime($a["dispatched"]) - strtotime($b["dispatched"]);
  }
}
usort($array, "sortByDispatched");
Sign up to request clarification or add additional context in comments.

5 Comments

What version of php are you using?
posted a solution that will work for older versions of php as well.
Yeah which each method I get an undefined variable a and b.
Put an "in the wild" example for you in the answer that should show how the code should be used.
Yeah, I just formatted a little different and got it working. Thanks a lot!
0

In additional to what Orangepill said, if you are using 5.2 or older, you can use the create_function() to create an anonymous (lambda-style) function instead. For example,

usort($array, create_function( '$a,$b', 
'return strtotime($a["dispatched"]) - strtotime($b["dispatched"]);'));

Comments

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.