自然排序多维数组

发布于 2024-09-19 02:57:38 字数 761 浏览 2 评论 0原文

我有一个像这样的多维数组:

array ([0] => array ([id] => 1 [name] => john doe [title] => Mr [days] => 10) 
[1] => array ([id] => 2 [name] => Joe Smith [title] => Dr [days] => 22) 
[2] => array ([id] => 3 [name] => John Jones [title] => Mr [days] => 3))

我需要对内部数组进行排序,以便数据按 days 键按自然顺序返回。

IE 是这样的:

array ([2] => array ([id] => 3 [name] => John Jones [title] => Mr [days] => 3)
[0] => array ([id] => 1 [name] => john doe [title] => Mr [days] => 10) 
[1] => array ([id] => 2 [name] => Joe Smith [title] => Dr [days] => 22))

我认为我需要的是一个通过 $key 将 natsort 应用于多维数组的函数,但到目前为止我还没有找到任何除了标准排序之外的函数。

有什么帮助吗?

I have an multidimensional array like this:

array ([0] => array ([id] => 1 [name] => john doe [title] => Mr [days] => 10) 
[1] => array ([id] => 2 [name] => Joe Smith [title] => Dr [days] => 22) 
[2] => array ([id] => 3 [name] => John Jones [title] => Mr [days] => 3))

I need to sort the inner arrays so that the data is returned in natural order by the days key.

I.E like this:

array ([2] => array ([id] => 3 [name] => John Jones [title] => Mr [days] => 3)
[0] => array ([id] => 1 [name] => john doe [title] => Mr [days] => 10) 
[1] => array ([id] => 2 [name] => Joe Smith [title] => Dr [days] => 22))

I think what I need is a function that applies natsort to a multidimensional array by $key, but so far I haven't been able to find any functions that do anything other than standard sorting.

Any help?

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

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

发布评论

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

评论(3

匿名的好友 2024-09-26 02:57:38

你想要的是 usort

您可以编写一个回调来为您进行比较:

usort($data, function($a, $b) {
    return ($a['days'] > $b['days'])
           ? 1 
           : ($a['days'] < $b['days'])
             ? -1 
             : 0;
});

免责声明:您需要 PHP 5.3.x 才能正常工作,否则您必须求助于 create_function 或预定义比较函数。

What you want is usort.

You can write a callback to do the comparison for you:

usort($data, function($a, $b) {
    return ($a['days'] > $b['days'])
           ? 1 
           : ($a['days'] < $b['days'])
             ? -1 
             : 0;
});

Disclaimer: You need PHP 5.3.x for this to work, else you have to resort to create_function or predefine the compare function.

羁客 2024-09-26 02:57:38

以下对我有用,

usort($array, function($a, $b) {
    return strnatcasecmp($a['days'], $b['days']);
});

从这里找到:- https://learntech.imsu.ox.ac.uk/blog/php-naturally-sorting-multidimension-arrays-by-string-values/

希望有人会发现这很有用。

The following worked for me,

usort($array, function($a, $b) {
    return strnatcasecmp($a['days'], $b['days']);
});

Found from here :- https://learntech.imsu.ox.ac.uk/blog/php-naturally-sorting-multidimension-arrays-by-string-values/

Hope someone will find this useful.

星星的轨迹 2024-09-26 02:57:38

有点不同的方法:

$days = array();
foreach($array as $key => $val) {
    $days[$key] = $val['days'];
}
array_multisort($days, $array); //$array being your input array

结果:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => Jay Doe
            [title] => Mr
            [days] => 3
        )

    [1] => Array
        (
            [id] => 1
            [name] => John Doe
            [title] => Mr
            [days] => 10
        )

    [2] => Array
        (
            [id] => 2
            [name] => Joe Doe
            [title] => Mr
            [days] => 22
        )

)

A bit of a different approach:

$days = array();
foreach($array as $key => $val) {
    $days[$key] = $val['days'];
}
array_multisort($days, $array); //$array being your input array

Result:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => Jay Doe
            [title] => Mr
            [days] => 3
        )

    [1] => Array
        (
            [id] => 1
            [name] => John Doe
            [title] => Mr
            [days] => 10
        )

    [2] => Array
        (
            [id] => 2
            [name] => Joe Doe
            [title] => Mr
            [days] => 22
        )

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