English 中文(简体)
“ 简单” 排序一个嵌套阵列, 使用数组_ 多元数或本地 PHP 函数而不是我自己的前方环
原标题:"Simple" sort a nested array using array_multisort or native PHP functions instead of my own foreach loop

我每周有以下数天,每天有时数(整个数组代表非全日雇员的时间表):

Array
    (
        [7] => Array
            (
                [0] => 15
                [1] => 14
                [2] => 13
                [3] => 11
                [4] => 12
                [5] => 10
            )

        [1] => Array
            (
                [0] => 10
                [1] => 13
                [2] => 12
            )

        [6] => Array
            (
                [0] => 14
            )

        [3] => Array
            (
                [0] => 4
                [1] => 5
                [2] => 6
            )

    )

我只需要:

  1. sort asc each sub-array (2nd dimension) - no need to maintain the numeric keys, values are integers
  2. sort asc the 1st dimension and maintain the numeric, integer keys

:

Array
    (
        [1] => Array
            (
                [0] => 10
                [1] => 12
                [2] => 13
            )

        [3] => Array
            (
                [0] => 4
                [1] => 5
                [2] => 6
            )

        [6] => Array
            (
                [0] => 14
            )

        [7] => Array
            (
                [0] => 10
                [1] => 11
                [2] => 12
                [3] => 13
                [4] => 14
                [5] => 15
            )

    )

附加信息 :

  1. only the keys of the 1st dimension and the values of the 2nd dimension (and of course their association) are meaningful to my use-case
  2. the 1st dimension can have at most 7 values, ranging from 1-7 (days of the week), and will have at least 1 value (1 day)
  3. the 2nd dimension can have at most 24 values, ranging from 0-23 (hours of each day), and will have at least 1 value (1 hour per day)

整个 < code>ksort 和 sort 每个二维数组的 < code> 都可以做到这一点:

ksort($sched);
foreach ($sched as &$array) sort($array);
unset($array);

但我希望我可以用本地的 php 阵列函数实现这一点。

我的搜索让我尝试了 arry_ multisort( array_ values ($array) 、 数组_ keys ($array) 、 $array) , 但我无法让它生效 。

最佳回答

这是我们所能做的最好的:

ksort($arr);
foreach ($arr as $k => $v)
  array_multisort($arr[$k]);
问题回答

一天结束时, 您真的要完成两个排序任务 。 第一个是按密钥对外部阵列进行排序。 第二个是按数值对内部阵列进行排序。 两个排序都是独立的 。

最佳的解决方案是您已经找到的 。 数组_ multisort () 将无法帮助您, 因为这是为了对实际上 < em> 依赖 < / em > 的对方进行排序, 比如“ 以姓排序 - 如果相等, 按名排序 ” 。 但应该避免它, 因为定义排序函数, 并使用 < code> usort () 和朋友, 更好地传达您实际排序的内容 。

沿着线条尝试

array_multisort(array_values($array), array_keys($array), ksort($array))

这可能会引起警告,但如果真正解决问题,您可以通过 < 强势> orror_ report(0) 来避免警告。





相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...