PHP数组使用子节点排序?

发布于 2024-09-28 06:08:31 字数 637 浏览 1 评论 0原文

好的,我有以下数组:)

Array
(
   [0] => Array
    (
        [id] => 6
        [name] => This Course
        [time] => 1288082700
        [description] => blah blah . 
        [link] => http://this.com/?g=5
        [course] => 22
    )

[1] => Array
    (
        [id] => 2
        [name] => Workshop
        [time] => 1287561600
        [description] => This description
        [link] => http://this.com/?g=5
        [session] => 8
        [course] => 23
        [type] => standard
        [adobelink] => 
    )

如何

使用内部“时间”键对整个数组进行排序?

谢谢!

Ok I have the following array:

Array
(
   [0] => Array
    (
        [id] => 6
        [name] => This Course
        [time] => 1288082700
        [description] => blah blah . 
        [link] => http://this.com/?g=5
        [course] => 22
    )

[1] => Array
    (
        [id] => 2
        [name] => Workshop
        [time] => 1287561600
        [description] => This description
        [link] => http://this.com/?g=5
        [session] => 8
        [course] => 23
        [type] => standard
        [adobelink] => 
    )

)

How can I sort this entire array by using the inner 'time' key ?

Thanks!

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

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

发布评论

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

评论(5

转角预定愛 2024-10-05 06:08:31

您可以将 usort 函数用作:

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

usort($arr,'cmp');

工作链接

You can use the usort function as:

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

usort($arr,'cmp');

Working link

烟─花易冷 2024-10-05 06:08:31

使用 usort()

此函数将使用用户提供的比较函数按数组的值对数组进行排序。如果您要排序的数组需要按某些重要标准进行排序,您应该使用此函数。

示例:

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

usort($array, 'cmp');

当然,如果数组没有 time 元素,这将会失败。接下来会发生什么取决于您的要求,所以我将把错误处理留给您;)

Use usort():

This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

Example:

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

usort($array, 'cmp');

Of course this will fail if an array has no time element. What should happen then depends on your requirements so I will leave the error handling to you ;)

云醉月微眠 2024-10-05 06:08:31

使用 PHP usort() 函数: http://php.net/manual/en/function。 usort.php

首先,定义一个函数来决定数据结构的比较结果:

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

然后调用 usort() 并将函数的 nmae 给它:

usort($array, "cmp");

你就完成了!

Use PHP usort() function: http://php.net/manual/en/function.usort.php

First, define a function that will decide on compare result for your data structure:

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

Then call usort() and give the nmae of your function to it:

usort($array, "cmp");

You're done!

我只土不豪 2024-10-05 06:08:31

uasort()维护您的密钥1

uasort($a, function($a, $b) {
    $a = $a['time']; // Assuming keys exist
    $b = $b['time'];
    if ($a == $b) {
        return 0;
    } else {
        return $a < $b ? -1 : 1; // Reverse < if sort order is wrong
    }
});

匿名函数语法需要 PHP 5.3+!如果 <5.3,则传递比较函数的名称(请参阅其他答案)。

1) 如果您也关心按键。如果没有,只需使用上面大量使用的 usort() 方法:) 比较函数基本相同(@codaddict 的 优雅的方法)。

uasort() will maintain your keys1.

uasort($a, function($a, $b) {
    $a = $a['time']; // Assuming keys exist
    $b = $b['time'];
    if ($a == $b) {
        return 0;
    } else {
        return $a < $b ? -1 : 1; // Reverse < if sort order is wrong
    }
});

Anonymous function syntax requires PHP 5.3+! Pass the name of the comparison function if <5.3 (see other answers).

1) In case you care about the keys, too. If not, just use the usort() approach found in abundance above :) The comparison function are basically identical (except for @codaddict's elegant approach).

紫南 2024-10-05 06:08:31
http://dk.php.net/usort

function sortByTime($a, $b)
{
 if ($a['time'] > $b['time'])
 {
  return 1;
 }
 else if ($a['time'] < $b['time'])
 {
  return -1;
 }
 return 0;
}

usort($yourArray, 'sortByTime');
http://dk.php.net/usort

function sortByTime($a, $b)
{
 if ($a['time'] > $b['time'])
 {
  return 1;
 }
 else if ($a['time'] < $b['time'])
 {
  return -1;
 }
 return 0;
}

usort($yourArray, 'sortByTime');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文