简体   繁体   English

对多维数组排序

[英]Sort a multidimensional array by

I want to sort the following multidimensional array by several keywords - let me explain in a simple way. 我想通过几个关键字对以下多维数组进行排序-让我以一种简单的方式进行说明。

this is how the parts in my multidimensional array look like 这就是我的多维数组中的部分的样子

[template] => Array
            (
                [0] => Array
                    (
                        [KeyA] => 123
                        [KeyB] => ABC
                        [KeyC] => #FFFFF
                        [custom] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => bla
                                        [var] => 2
                                    )

                                [1] => Array
                                    (
                                        [value] => c1
                                        [var] => 5
                                    )

                            )

                    )
            )

there are tons of multidimensional arrays in that template array and I want them now to sort for example by KeyC (#00000 first prio, #FFFFFF second prio, #333333 third prio) and then by KeyA alphabetic. 在该模板数组中有大量的多维数组,我现在希望它们按KeyC排序(#00000第一个prio,#FFFFFF第二个prio,#333333第三个prio),然后按KeyA字母排序。

How to do that ? 怎么做 ?

PHP has several functions that deal with sorting arrays and the logic behind array sorting are: PHP具有处理数组排序的几个函数,数组排序背后的逻辑是:

  • Some sort based on the array keys, whereas others by the values: $array['key'] = 'value'; 某些排序基于数组键,而其他排序则基于值:$ array ['key'] ='value';

  • Whether or not the correlation between the keys and values are maintained after the sort, which may mean the keys are reset numerically (0,1,2 ...) 排序后是否保留键和值之间的相关性,这可能意味着键会被数字重置(0,1,2 ...)

  • The order of the sort: alphabetical, low to high (ascending), high to low (descending), numerical, natural, random, or user defined 排序顺序:字母,低到高(升序),高到低(降序),数字,自然,随机或用户定义

  • If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable). 如果这些排序函数中的任何一个对两个成员求值均相等,则顺序不确定(排序不稳定)。

Note: All of these sort functions act directly on the array variable itself, as opposed to returning a new sorted array. 注意:所有这些排序函数都直接作用于数组变量本身,而不是返回新的排序数组。

The main functions of sorting are described http://www.php.net/manual/en/array.sorting.php and for creating custom sorting you have to tricks best suitable to yourself. http://www.php.net/manual/en/array.sorting.php中介绍了排序的主要功能,并且要创建自定义排序,您必须技巧最适合自己的技巧。

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

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