在php中按值排序数组数组

时间:2013-10-07 07:08:16

标签: php sorting

我有一个名为$ result的数组,其内容类似于:

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_started_date 的基础上对其进行排序。我尝试使用kso​​rt,asort等但没有运气,也许我没有正确使用它。任何帮助都将受到高度赞赏。

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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;
}