在用PHP 3排序第2个嵌套数组按值 [英] Sort 2nd nested array by value in 3rd with php

查看:134
本文介绍了在用PHP 3排序第2个嵌套数组按值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要排序值n第三用php第二嵌套数组; '开始时间'。也许思考usort是关键。发布的@week @day @class嵌套阵列,以及一个usort调用和功能我一直在试图上的变化。

Need to sort second nested array by a value n the third with php; 'Start Time'. Thinking maybe usort is the key. Posting the nested arrays of @week @day @class as well as a usort call and function I've been trying variations on.

Array
(
  [Sunday] => Array
    (
        [0] => Array
            (
                [Class Name] => Fun Class
                [Instructor] => Fun Teacher
                [Class Type] => Yoga Stretches
                [Start Time] => 1899-12-30T17:00:00
                [End Time] => 1899-12-30T18:15:00
                [Day Name] => Sunday
            )

        [1] => Array
            (
                [Class Name] => Another Fun Class
                [Instructor] => Same Fun Teacher
                [Class Type] => Relaxation
                [Start Time] => 1899-12-30T18:30:00
                [End Time] => 1899-12-30T19:45:00
                [Day Name] => Sunday
            )

        [2] => Array
            (
                [Class Name] => Hot Yoga without Cold
                [Instructor] => Dave the Flexible
                [Class Type] => Hot Hot Yoga
                [Start Time] => 1899-12-30T09:00:00
                [End Time] => 1899-12-30T10:30:00
                [Day Name] => Sunday
            )
    )
    [Monday] => Array
    (
        [0] => Array
            (
                [Class Name] => Mellow Chill Hang
                [Instructor] => Guru Awesome
                [Class Type] => Yoga Namaste
                [Start Time] => 1899-12-30T12:00:00
                [End Time] => 1899-12-30T13:00:00
                [Day Name] => Monday
            )
   )
)

试过(上变化):

Have tried (variations on):

foreach ($week_schedule as $day)//$week_schedule = the outer array
    {
     for($j=count($week_schedule);$j>=0;$j--)
         usort($day,'sortTimes');
}

function sortTimes( $a, $b ) {
    return strtotime($a["Start Time"]) - strtotime($b["Start Time"]);
 }

在使用一个单一的foreach与提取值($天=> $类)的尝试。
我敢肯定,这不会最终需要大量code。

Have tried using a single foreach with extracting values ($day => $class). I bet this won't end up requiring much code.

推荐答案

array_map 尝试以进入嵌套的元素和启动时间有一个'T'字,我认为不能与的strtotime 所以我删除了:

Try with array_map to get into the nested elements and your Start Time has a 'T' character which I think cannot be used with strtotime so I've removed it:

function custom_map( $array ) {
    usort( $array, function( $a, $b ) {
        return ($a['Start Time']<$b['Start Time'])? -1 : 1;
    });
    return $array;
}

$new_array = array_map( "custom_map", $week_schedule );

var_dump($new_array);

演示

Demo

既然你有兴趣了解这是如何工作的,我做了一些研究自己和这个解释提出了:

Since you were interested in learning how this works, I did a few research myself and came up with this explanation:

首先,你应该知道array_map()和usort()是。

First of all, you should know what array_map() and usort() are for.

array_map()从PHP文件:

array_map()返回一个包含了array1的所有元素的数组
  应用回调函数,以各一个。

array_map() returns an array containing all the elements of array1 after applying the callback function to each one.

语法:

array array_map ( callable $callback , array $array1 [, array $... ] )

在简单的话,array_map()将通过您的阵列到您指定的回调函数的每个元素。它返回一个数组(用更改后的值,您在做了的的回调函数)。

In simple words, array_map() will pass each element of your array to the callback function that you specify. And it returns an array (with the changed values, that you have done in that callback function).

现在usort()

usort()从PHP文件:

此功能将整理使用用户提供的比较函数的值的数组。如果你想需要排序的数组由一些非平凡的标准进行排序,则应该使用此功能。

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.

语法:

bool usort ( array &$array , callable $value_compare_func )

所以,usort()基本上每个数组元素中,以便它以正确的顺序进行排序的。你在这里看到的行:

So, usort() basically compares each array element in order to sort it in the correct order. The line that you see here:

return ($a['Start Time']<$b['Start Time'])? -1 : 1;

基本上可以翻译为:

basically translates as:

 if $a['Start Time'] is lesser than $b['Start Time'], then return -1 so
 that $a can come before $b

与值:

$a['Start Time'] = '1899-12-30T09:00:00'; //3rd element
$b['Start Time'] = '1899-12-30T17:00:00'; //1st element

现在显然,美元的这是第三个因素是比第一元素$ B较小,因此$ a将会在它之前的位置。 usort()使对证从开始每个组合的顺序来排序结束。

Now clearly, $a which is the third element is lesser than the 1st element $b, thus $a will be positioned before it. usort() makes check against every combination from start to end in order to sort.

现在,回来array_map(),我们使用它的原因是因为你有嵌套数组。

Now, coming back to array_map(), the reason we are using it is because you have nested arrays.

在array_map()实际上做的是什么,通过所有的@days到usort。然后usort通吃的@class和排序相应。如果你看到下面的演示,你的@weeks已采取了只@days和@class present,上述code将无法正常工作,并把你扔开始时间的错误不会被发现,因为你试图通过2个嵌套数组来遍历时仅1 present:

What the array_map() actually does is, passes all your @days to the usort. And then the usort takes all your @class and sorts accordingly. If you see the following demo, where your @weeks have been taken out only @days and @class present, the above code will not work and will throw you errors of 'Start Time' not being found because you are trying to traverse through 2 nested arrays when only 1 is present:

演示

Demo

有关上述结构,正确的方法将是直接实现usort无需使用array_map像

For the above structure, the right method will be to implement usort directly without using array_map like:

演示

Demo

这篇关于在用PHP 3排序第2个嵌套数组按值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆