简体   繁体   中英

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.

How to do that ?

PHP has several functions that deal with sorting arrays and the logic behind array sorting are:

  • Some sort based on the array keys, whereas others by the values: $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 ...)

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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