按键排序数组

时间:2021-11-03 21:32:44

i have an array like this:

我有一个像这样的数组:

Array
(
    [0] => Array
        (
            [title] => some title
            [time] => 1279231500
        )

    [1] => Array
        (
            [title] => some title 2
            [time] => 1279231440
        )

    [2] => Array
        (
            [title] => some title 3
            [time] => 1279229880
        )
)

how i can sort it based on time?

我如何根据时间对其进行排序?

2 个解决方案

#1


4  

You can sort it this way (since it is an associative array):

你可以这样排序(因为它是一个关联数组):

function cmp($a, $b)
{
   return strcmp($a['time'], $b['time']);
}

usort($your_array, "cmp");
print_r($your_array);

#2


1  

As Gumbo mentioned, you should not use strcmp for integer values.

正如Gumbo所提到的,你不应该使用strcmp作为整数值。

Use this function

使用此功能

function cmp($a, $b) {
    if ($a['time'] == $b['time'])
        return 0;
    return ($a['time'] < $b['time']) ? -1 : 1;
}

#1


4  

You can sort it this way (since it is an associative array):

你可以这样排序(因为它是一个关联数组):

function cmp($a, $b)
{
   return strcmp($a['time'], $b['time']);
}

usort($your_array, "cmp");
print_r($your_array);

#2


1  

As Gumbo mentioned, you should not use strcmp for integer values.

正如Gumbo所提到的,你不应该使用strcmp作为整数值。

Use this function

使用此功能

function cmp($a, $b) {
    if ($a['time'] == $b['time'])
        return 0;
    return ($a['time'] < $b['time']) ? -1 : 1;
}