English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A função array_multisort() do PHP ordena múltiplos arrays ou arrays multidimensionais
array_multisort(array1,ordem de ordenação, tipo de ordenação, array2...);
array_multisort() pode ser usado para ordenar múltiplos arrays em uma só vez ou para ordenar arrays multidimensionais com base em uma ou várias dimensões.
O nome da chave associado (string) permanece inalterado, mas os nomes dos índices numéricos serão reindexados.
Número | Parâmetros e descrição |
---|---|
1 | array1(obrigatório) It specifies an array |
2 | ordem de ordenação (opcional) Especifica a ordem de classificação. Valores possíveis-
|
3 | type de ordenação (opcional) Ao comparar elementos, ele especifica o tipo a ser usado. Valores possíveis:
|
4 | array2(Optional) It specifies an array |
Success, returns TRUE; failure, returns FALSE.
1、Multiple arrays sorting example
<?php $input1 = array("10" 100, 100, "a"); $input2 = array(1, 3",2" 1); array_multisort($input1, $input2); print_r($input1); print_r($input2); ?>Test and see ‹/›
Output result:
Array ( [0] => 10 [1]] => a [2]] => 100 [3]] => 100 ) Array ( [0] => 1 [1]] => 1 [2]] => 2 [3]] => 3 )
2、Sorting multidimensional arrays example
<?php $ar = array( array("10" 11, 100, 100, "a"), array( 1, 2",2" 3, 1) ); array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); var_dump($ar); ?>Test and see ‹/›
Output result:
array(2) { [0] => array(5) { [0] => string(2) "10" [1]] => int(100) [2]] => int(100) [3]] => int(11) [4]] => string(1)] "a" } [1]] => array(5) { [0] => int(1) [1]] => int(3) [2]] => string(1) "2" [3]] => int(2) [4]] => int(1) } }
3、Case-insensitive sorting of arrays
<?php $array = array('Alpha', 'atomic', 'Beta', 'bank'); $array_lowercase = array_map('strtolower', $array); array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array); print_r($array); ?>Test and see ‹/›
Output result:
Array ( [0] => Alpha [1]] => atomic [2]] => bank [3]] => Beta )