Função array_multisort() do PHP

Exemplo

Retorna um array classificado em ordem crescente:

<?php
$a = array("Cão", "Gato", "Cavalo", "Urso", "Zebra");
array_multisort($a);;
print_r($a);
?>

Executar exemplo

Definição e uso

A função array_multisort() retorna um array ordenado. Você pode inserir um ou mais arrays. A função primeiro ordena o primeiro array, seguido por outros arrays, se dois ou mais valores forem iguais, ela ordena o próximo array.

Note:String keys will be retained, but numeric keys will be reindexed starting from 0 and incremented by 1.

Note:You can set the sorting order and sorting type parameters after each array. If not set, each array parameter will use the default value.

Syntax

array_multisort(array1,sorting order,sorting type,array2,array3...)
Parameters Description
array1 Required. Specify the array.
sorting order

Optional. Specify the sorting order. Possible values:

  • SORT_ASC - Default. Order in ascending order (A-Z).
  • SORT_DESC - Order in descending order (Z-A).
sorting type

Optional. Specify the sorting type. Possible values:

  • SORT_REGULAR - Default. Order each item in regular order (Standard ASCII, do not change type).
  • SORT_NUMERIC - Treat each item as a number.
  • SORT_STRING - Treat each item as a string.
  • SORT_LOCALE_STRING - Treat each item as a string, based on the current locale setting (can be changed via setlocale()).
  • SORT_NATURAL - Treat each item as a string, using a natural sorting similar to natsort().
  • SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings without case sensitivity.
array2 Optional. Specify the array.
array3 Optional. Specify the array.

Description

The array_multisort() function sorts multiple arrays or multidimensional arrays.

The array in the parameter is treated as a table column and sorted by row - this is similar to the functionality of the ORDER BY clause in SQL. The first array is the main array to be sorted. If the rows (values) in the array are compared as the same, they will be sorted according to the size of the corresponding values in the next input array, and so on.

The first parameter is an array, and each subsequent parameter may be an array or one of the following sorting order flags (sorting flags are used to change the default sorting order):

  • SORT_ASC - Default, order in ascending order. (A-Z)
  • SORT_DESC - Order in descending order. (Z-A)

You can specify the sorting type after that:

  • SORT_REGULAR - Default. Order each item in regular order.
  • SORT_NUMERIC - Order each item numerically.
  • SORT_STRING - Classificar cada item em ordem alfabética.

Detalhes técnicos

Retorno: Retorna TRUE se bem-sucedido, retorna FALSE se falhar.
Versão PHP: 4+
Log de atualizações:

Os tipos de classificação SORT_NATURAL e SORT_FLAG_CASE foram adicionados no PHP 5.4.

O tipo de classificação SORT_LOCALE_STRING foi adicionado no PHP 5.3.

Mais exemplos

Exemplo 1

Retorna um array classificado em ordem crescente:

<?php
$a1=array("Dog","Cat");
$a2=array("Fido","Missy");
array_multisort($a1,$a2);;
print_r($a1);
print_r($a2);
?>

Executar exemplo

Exemplo 2

Como classificar quando dois valores são iguais:

<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);;
print_r($a1);
print_r($a2);
?>

Executar exemplo

Exemplo 3

Usar parâmetros de classificação:

<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);;
print_r($a1);
print_r($a2);
?>

Executar exemplo

Exemplo 4

Unir dois arrays e classificar em ordem decrescente por número:

<?php
$a1=array(1,30,15,7,25);
$a2=array(4,30,20,41,66);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);;
print_r($num);
?>

Executar exemplo