按列值对二维数组的行进行排序

发布于 2024-09-17 07:17:26 字数 422 浏览 1 评论 0原文

我有一个这样的数组:

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-09-24 07:17:27

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

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

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

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);
惟欲睡 2024-09-24 07:17:27

正如 Gumbo 提到的,您不应该将 strcmp 用于整数值。

使用此功能

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

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

Use this function

function cmp($a, $b) {
    if ($a['time'] == $b['time'])
        return 0;
    return ($a['time'] < $b['time']) ? -1 : 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文