简体   繁体   English

PHP 多维数组按值排序

[英]PHP multidimensional array sort by value

Regarding this multidimensional array:关于这个多维数组:

[
(int) 7 => [
    (int) 0 => [
        (int) 0 => '12:45',
        (int) 1 => 'E1',
        (int) 2 => 'B EXTREME 30'
    ],
    (int) 1 => [
        (int) 0 => '10:15',
        (int) 1 => 'E1',
        (int) 2 => 'B SHAPE 30'
    ],
],
(int) 1 => [
    (int) 0 => [
        (int) 0 => '09:30',
        (int) 1 => 'E2',
        (int) 2 => 'CYCLING VIRTUAL 50'
    ],
    (int) 1 => [
        (int) 0 => '10:30',
        (int) 1 => 'E1',
        (int) 2 => 'BODY PUMP VIRTUAL 60'
    ],
    (int) 2 => [
        (int) 0 => '11:45',
        (int) 1 => 'E1',
        (int) 2 => 'BODY BALANCE VIRTUAL 60'
    ],
],
(int) 2 => [
    (int) 0 => [
        (int) 0 => '14:45',
        (int) 1 => 'E2',
        (int) 2 => 'CYCLING VIRTUAL 50'
    ],
    (int) 1 => [
        (int) 0 => '17:00',
        (int) 1 => 'E1',
        (int) 2 => 'POSTURA ALONGAMENTO 60'
    ],
    (int) 2 => [
        (int) 0 => '09:15',
        (int) 1 => 'E1',
        (int) 2 => 'BODY PUMP 50'
    ],
]
]

The first key, of each first level array, are days of the week (day 7, day 1 and day 2).每个第一级数组的第一个键是一周中的几天(第 7 天、第 1 天和第 2 天)。

The arrays inside each first level array contain hour ( 09:45 ), rooms ( E1 ) and description ( B EXTREME 30 ).每个第一级数组内的数组包含小时 ( 09:45 )、房间 ( E1 ) 和描述 ( B EXTREME 30 )。

I tried to sort this multidimensional array by the second levels array hour value.我试图通过第二级数组小时值对这个多维数组进行排序。 I used usort() , ksort() , array_multisort() , and some custom made functions for sorting the array as i need without luck.我使用了usort()ksort()array_multisort()和一些定制的函数来根据需要对数组进行排序,但没有运气。

The inside arrays must be sorted by ascending order, like this (example with day 2):内部数组必须按升序排序,如下所示(第 2 天的示例):

09:15 -> 14:45 -> 17:00

Does anyone knows how can i achieve this?有谁知道我怎样才能做到这一点?

Assuming your data is called $data .假设您的数据称为$data Iterate the outer array, and apply a sort on each mid-level array, based on the time-part (in the innermost arrays).迭代外部数组,并根据时间部分(在最里面的数组中)对每个中级数组应用排序。 As your times are always formatted as "hh:ss", a string comparison in the usort callback does the job:由于您的时间总是格式化为“hh:ss”,因此usort回调中的字符串比较可以完成这项工作:

foreach ($data as &$events) {
    usort($events, function($a, $b) {
        return strcmp($a[0], $b[0]);
    });
}

Note the & in the foreach : this makes sure you sort the original data, and not a copy of it.注意foreach& :这确保您对原始数据进行排序,而不是对它的副本进行排序。

If you want to create a new array, let's say $result , then do this (no & here!):如果你想创建一个新的数组,假设$result ,那么做到这一点(不&这里!):

foreach ($data as $day => $events) {
    usort($events, function($a, $b) {
        return strcmp($a[0], $b[0]);
    });
    $result[$day] = $events;
}

usort — Sort an array by values using a user-defined comparison function. usort — 使用用户定义的比较函数按值对数组进行排序。 Lets create a function where we compare time of two events.让我们创建一个函数来比较两个事件的时间。

This is part of your array这是你的阵列的一部分

$array = [
    2 => [
        0 => [
            0 => '14:45',
            1 => 'E2',
            2 => 'CYCLING VIRTUAL 50'
        ],
        1 => [
            0 => '17:00',
            1 => 'E1',
            2 => 'BODY PUMP VIRTUAL 60'
        ],
        2 => [
            0 => '09:15',
            1 => 'E1',
            2 => 'BODY BALANCE VIRTUAL 60'
        ],
    ],
];

This is an example how to sort items for one day (day 2)这是如何对一天(第 2 天)的项目进行排序的示例

usort($array[2], function ($a, $b) {
    $time_a = strtotime($a[0]); // convert string to a unix timestamp to compare int
    $time_b = strtotime($b[0]);
    return $time_a - $time_b;
});

Output using print_r($array);使用print_r($array);输出print_r($array);

Array
(
    [2] => Array
        (
            [0] => Array
                (
                    [0] => 09:15
                    [1] => E1
                    [2] => BODY BALANCE VIRTUAL 60
                )

            [1] => Array
                (
                    [0] => 14:45
                    [1] => E2
                    [2] => CYCLING VIRTUAL 50
                )

            [2] => Array
                (
                    [0] => 17:00
                    [1] => E1
                    [2] => BODY PUMP VIRTUAL 60
                )

        )

)

To sort all days we do in a loop, passing each day array as a reference using & :为了对我们在循环中所做的所有日子进行排序,使用&传递每一天数组作为参考:

foreach ($array as &$day) {
    usort($day, function ($a, $b) {
        $time_a = strtotime($a[0]);
        $time_b = strtotime($b[0]);
        return $time_a - $time_b;
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM