-2

I have an array named as $result which have contents something like this:

Array (    
        [1114_435] => stdClass Object
        (
            [uid] => 435
            [v_first_name] => fHerter
            [v_last_name] => Herter
            [id] => 1114
            [v_title] => Morning Stretch
            [fk_resident_id] => 435
            [v_location] => Front Lawn
            [i_started_date] => 1357054200
        )

[1114_444] => stdClass Object
    (
        [uid] => 444
        [v_first_name] => fXYZ
        [v_last_name] => XYZ
        [id] => 1114
        [v_title] => Morning Stretch
        [fk_resident_id] => 444
        [v_location] => Front Lawn
        [i_started_date] => 1357054200
    )

[1114_448] => stdClass Object
    (
        [uid] => 448
        [v_first_name] => fDavidson
        [v_last_name] => Davidson
        [id] => 1114
        [v_title] => Dinner
        [fk_resident_id] => 448
        [v_location] => Front Lawn
        [i_started_date] => 1357051000
    )
)

I want to sort it on the basis of i_started_date. I tried using ksort, asort etc but no luck, maybe i wasn`t using it properly. Any help would be highly appreciated.

Thanks!

leedz
  • 61
  • 1
  • 5
  • So what is your approach? Hint: try `usort()` – Alma Do Oct 07 '13 at 07:11
  • As Alma says, try making an approach for yourself first. We have to see how far you've tried it yourself. – MichaelP Oct 07 '13 at 07:14
  • `function cmp($a, $b) { if ($a['i_started_date'] == $b['i_started_date']) { return 0; } return ($a['i_started_date'] < $b['i_started_date']) ? -1 : 1; }` Its an object, i cannot use it as an array. This did not work for me, nor did any other functions that are available for sorting of arrays! – leedz Oct 07 '13 at 08:24
  • Have you actually *read* and *understood* this: http://stackoverflow.com/a/17364128/476 ?! – deceze Oct 07 '13 at 09:03

1 Answers1

1

Try something like this:

function sortArray($data)
{

    $sortArray = array();

    foreach($data as $dt)
    {
        foreach($dt as $key=>$value)
        {
            if(!isset($sortArray[$key]))
            {
                $sortArray[$key] = array();
            }

        $sortArray[$key][] = $value;
        }
    }

    $orderby = "1"; //change this to whatever key you want from the array
    array_multisort($sortArray[$orderby],SORT_ASC,$data);

return $data;
}
andrew
  • 2,058
  • 2
  • 25
  • 33