English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriais básicos PHP

Tutoriais avançados PHP

PHP & MySQL

Manual de referência PHP

Uso e exemplo da função array_multisort() do PHP

PHP Array Function Manual

A função array_multisort() do PHP ordena múltiplos arrays ou arrays multidimensionais

Sintaxe

array_multisort(array1,ordem de ordenação, tipo de ordenação, array2...);

Definição e uso

 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.

Parâmetro

NúmeroParâ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-

  • SORT_ASC Padrão. Ordem crescente (A-Z)

  • SORT_DESC Ordem decrescente (Z-A)

3

type de ordenação (opcional)

Ao comparar elementos, ele especifica o tipo a ser usado. Valores possíveis:

  • SORT_REGULAR - Comparação de itens pelo método usual (sem modificar o tipo)

  • SORT_NUMERIC - Comparação de tamanho numérico

  • SORT_STRING - Comparação de strings

  • SORT_LOCALE_STRING - Sort strings according to the current locale settings. It will use locale information, which can be modified by setlocale().

  • SORT_NATURAL - Sort strings in 'natural order', similar to natsort()

  • SORT_FLAG_CASE - Can be combined (bitwise OR) SORT_STRING or SORT_NATURAL for case-insensitive string sorting.

4

array2(Optional)

It specifies an array

Return value

Success, returns TRUE; failure, returns FALSE.

Online examples

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
)

 PHP Array Function Manual