PHP Conference Kansai 2025

array_multisort

(PHP 4, PHP 5, PHP 7, PHP 8)

array_multisortÇok sayıda diziyi veya çok boyutlu dizileri sıralar

Açıklama

array_multisort(
    array &$dizi1,
    mixed $dizi1_sıralama_düzeni = SORT_ASC,
    mixed $dizi1_sıralama_seçenekleri = SORT_REGULAR,
    mixed ...$diğerleri
): bool

array_multisort() işlevi çok sayıda diziyi bir kerede sıralayabileceği gibi çok boyutlu bir diziyi bir veya daha fazla boyuta göre de sıralayabilir.

İlişkisel (string) anahtarlar korunursa da sayısal indisler yeniden oluşturulur.

Bilginize:

İki üye karşılaştırıldığında eşitse bunların özgün sıralamadaki yerleri korunur. PHP 8.0.0 öncesinde sıralı dizideki göreli yerleri tanımsızdı.

Bilginize:

Dahili dizi göstericisi ilk elemana atanır.

Bağımsız Değişkenler

dizi

Sıralanacak dizi.

dizi1_sıralama_düzeni

Önceki dizi bağımsız değişkenini sıralamada kullanılacak düzen. Artan şekilde sıralamak için SORT_ASC, azalan şekilde sıralalmak için SORT_DESC.

Bu bağımsız değişken dizi1_sıralama_seçenekleri bağımsız değişkeniyle yer değiştirebilir. Bu durumda sıralama düzeni olarak SORT_ASC öntanımlıdır.

dizi1_sıralama_seçenekleri

Önceki dizi bağımsız değişkeni için sıralama seçenekleri

Sıralama türü seçenekleri:

Bu bağımsız değişken dizi1_sıralama_düzeni ile yer değiştirebilir veya hiç belirtilmeyebilir; bu durumda SORT_REGULAR öntanımlıdır.

diğerleri

Dğer diziler; ardından isteğe bağlı oarak sıralama düzeni ve seçenekleri belirtilebilir. Yalnızca önceki dizilerdeki eşdeğer öğelere karşılık gelen öğeler karşılaştırılır. Başka bir deyişle, sıralama sözlükbilimseldir.

Dönen Değerler

Başarı durumunda true, başarısızlık durumunda false döner.

Örnekler

Örnek 1 - Çok sayıda diziyi sıralamak

<?php
$ar1
= array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

Bu örnekte sıralama sonrasında iki dizi arasındaki elemanlar yerlerine göre birbirinin karşıtı olmak üzere ilk dizi 0, 10, 100, 100; ikinci dizi 4, 1, 2, 3 içerir.

array(4) {
  [0]=> int(0)
  [1]=> int(10)
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(4)
  [1]=> int(1)
  [2]=> int(2)
  [3]=> int(3)
}

Örnek 2 - Çok boyutlu diziyi sıralamak

<?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);
?>

Bu örnekte sıralama sonrasında, ilk dizi "10", 100, 100, 11, "a" dizisine dönüşecek (artan düzende dizgesel olarak sıralanır), ikinci dizi ise 1, 3, "2", 2, 1 değerlerini içerecektir (azalan düzende sayısal olarak sıralanır) .

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)
  }
}

Örnek 3 - Veritabanı sonuçlarının sıralanması

Bu örnek için, veri dizisindeki her eleman tablodaki bir satıra karşılıktır.

Örnek veri:

bölüm |   baskı
-------+--------
    67 |       2
    86 |       1
    85 |       6
    98 |       2
    86 |       6
    67 |       7

Dizi olarak veri dizisi kullanılmıştır. Aynı sonuç mysqli_fetch_assoc() işlevini bir döngü içinde kullanarak da elde edilebilir.

<?php
$veri
[] = array('bölüm' => 67, 'baskı' => 2);
$veri[] = array('bölüm' => 86, 'baskı' => 1);
$veri[] = array('bölüm' => 85, 'baskı' => 6);
$veri[] = array('bölüm' => 98, 'baskı' => 2);
$veri[] = array('bölüm' => 86, 'baskı' => 6);
$veri[] = array('bölüm' => 67, 'baskı' => 7);
?>

Bu örnekte bölüm'ü azalan baskı'yı artan düzende sıralayacağız.

Satırlara karşılık gelen bir dizimiz var, fakat array_multisort() sütun dizileri gerektiriyor. Bu nedenle sıralamayı yapmadan önce sütunları elde etmek için şu kodu kullanacağız:

<?php
// Sütun listelerini elde edelim
foreach ($veri as $anahtar => $satır) {
$bölüm[$anahtar] = $satır['bölüm'];
$baskı[$anahtar] = $satır['baskı'];
}

// Yukarıdaki kod yerine array_column() kullanılabilir
$volume = array_column($veri, 'bölüm');
$edition = array_column($veri, 'baskı');


// bölüm'ü azalan, baskı'yı artan düzende sıralayalım ve
// ortak bir anahtara göre sıralamak için $veri'yi son
// bağımsız değişken olarak ekleyelim
array_multisort($bölüm, SORT_DESC, $baskı, SORT_ASC, $veri);
?>

Veri kümemiz artık sıraya dizildi:

bölüm | baskı
------+------
   98 |     2
   86 |     1
   86 |     6
   85 |     6
   67 |     2
   67 |     7

Örnek 4 - Harf büyüklüğüne duyarsız sıralama

SORT_STRING ve SORT_REGULAR seçenekleri harf büyüklüğüne duyarlı olup, büyük harf ile başlayan dizgeler sıralamada küçük harfle başlayanlardan önce gelir.

Harf büyüklüğüne duyarsız sıralama yapmak için özgün dizinin önce küçük harfli bir kopyası oluşturulur.

<?php
$dizi
= array('Alpha', 'atomic', 'Beta', 'bank');
$küçük_harfli_dizi = array_map('strtolower', $array);

array_multisort($küçük_harfli_dizi, SORT_ASC, SORT_STRING, $dizi);

print_r($array);
?>

Yukarıdaki örneğin çıktısı:

Array
(
    [0] => Alpha
    [1] => atomic
    [2] => bank
    [3] => Beta
)

Ayrıca Bakınız

add a note

User Contributed Notes 7 notes

up
225
jimpoz at jimpoz dot com
14 years ago
I came up with an easy way to sort database-style results. This does what example 3 does, except it takes care of creating those intermediate arrays for you before passing control on to array_multisort().

<?php
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
foreach (
$args as $n => $field) {
if (
is_string($field)) {
$tmp = array();
foreach (
$data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return
array_pop($args);
}
?>

The sorted array is now in the return value of the function instead of being passed by reference.

<?php
$data
[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

// Pass the array, followed by the column names and sort flags
$sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);
?>
up
74
matt at bosc dot io
9 years ago
One-liner function to sort multidimensionnal array by key, thank's to array_column

<?php

array_multisort
(array_column($array, 'key'), SORT_DESC, $array);

?>
up
91
cagret at gmail dot com
15 years ago
A more inuitive way of sorting multidimensional arrays using array_msort() in just one line, you don't have to divide the original array into per-column-arrays:

<?php

$arr1
= array(
array(
'id'=>1,'name'=>'aA','cat'=>'cc'),
array(
'id'=>2,'name'=>'aa','cat'=>'dd'),
array(
'id'=>3,'name'=>'bb','cat'=>'cc'),
array(
'id'=>4,'name'=>'bb','cat'=>'dd')
);

$arr2 = array_msort($arr1, array('name'=>SORT_DESC, 'cat'=>SORT_ASC));

debug($arr1, $arr2);

arr1:
0:
id: 1 (int)
name: aA (string:2)
cat: cc (string:2)
1:
id: 2 (int)
name: aa (string:2)
cat: dd (string:2)
2:
id: 3 (int)
name: bb (string:2)
cat: cc (string:2)
3:
id: 4 (int)
name: bb (string:2)
cat: dd (string:2)
arr2:
2:
id: 3 (int)
name: bb (string:2)
cat: cc (string:2)
3:
id: 4 (int)
name: bb (string:2)
cat: dd (string:2)
0:
id: 1 (int)
name: aA (string:2)
cat: cc (string:2)
1:
id: 2 (int)
name: aa (string:2)
cat: dd (string:2)

function
array_msort($array, $cols)
{
$colarr = array();
foreach (
$cols as $col => $order) {
$colarr[$col] = array();
foreach (
$array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
}
$eval = 'array_multisort(';
foreach (
$cols as $col => $order) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr($eval,0,-1).');';
eval(
$eval);
$ret = array();
foreach (
$colarr as $col => $arr) {
foreach (
$arr as $k => $v) {
$k = substr($k,1);
if (!isset(
$ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return
$ret;

}

?>
up
47
Robert C
11 years ago
Hi,

I would like to see the next code snippet to be added to http://nl3.php.net/array_multisort

Purpose: Sort a 2-dimensional array on some key(s)

Advantage of function:
- uses PHP's array_multisort function for sorting;
- it prepares the arrays (needed by array_multisort) for you;
- allows the sort criteria be passed as a separate array (It is possible to use sort order and flags.);
- easy to set/overwrite the way strings are sorted (case insensitive instead of case sensitive, which is PHP's default way of sorting);
- performs excellent

function MultiSort($data, $sortCriteria, $caseInSensitive = true)
{
if( !is_array($data) || !is_array($sortCriteria))
return false;
$args = array();
$i = 0;
foreach($sortCriteria as $sortColumn => $sortAttributes)
{
$colList = array();
foreach ($data as $key => $row)
{
$convertToLower = $caseInSensitive && (in_array(SORT_STRING, $sortAttributes) || in_array(SORT_REGULAR, $sortAttributes));
$rowData = $convertToLower ? strtolower($row[$sortColumn]) : $row[$sortColumn];
$colLists[$sortColumn][$key] = $rowData;
}
$args[] = &$colLists[$sortColumn];

foreach($sortAttributes as $sortAttribute)
{
$tmp[$i] = $sortAttribute;
$args[] = &$tmp[$i];
$i++;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return end($args);
}

Usage:

//Fill an array with random test data
define('MAX_ITEMS', 15);
define('MAX_VAL', 20);
for($i=0; $i < MAX_ITEMS; $i++)
$data[] = array('field1' => rand(1, MAX_VAL), 'field2' => rand(1, MAX_VAL), 'field3' => rand(1, MAX_VAL) );

//Set the sort criteria (add as many fields as you want)
$sortCriteria =
array('field1' => array(SORT_DESC, SORT_NUMERIC),
'field3' => array(SORT_DESC, SORT_NUMERIC)
);

//Call it like this:
$sortedData = MultiSort($data, $sortCriteria, true);
up
21
nick ([AT]) nickyost ([DOT]) com
13 years ago
USort function can be used to sort multidimensional arrays with almost no work whatsoever by using the individual values within the custom sort function.

This function passes the entire child element even if it is not a string. If it is an array, as would be the case in multidimensional arrays, it will pass the whole child array as one parameter.

Therefore, do something elegant like this:

<?php
// Sort the multidimensional array
usort($results, "custom_sort");
// Define the custom sort function
function custom_sort($a,$b) {
return
$a['some_sub_var']>$b['some_sub_var'];
}
?>

This does in 4 lines what other functions took 40 to 50 lines to do. This does not require you to create temporary arrays or anything. This is, for me, a highly preferred solution over this function.

Hope it helps!
up
3
Magento-User
11 years ago
When sorting an array of (complex) objects, this function can give you a "Fatal error: Nesting level too deep" since it directly compares elements in later arrays if the elements in earlier ones compare equal. This can be worked around with the Flag-Parameter:
<?php
$sortKeys
= array_map($extractKey, $lotsOfComplexObjects);
array_multisort($sortKeys, $lotsOfComplexObjects, SORT_ASC, SORT_NUMERIC);
?>
I'm replacing an 'uasort()'-call which is significantly slower since it leads to a lot of calls to the comparison-function but most of the objects involved are recursive.

If this 'trick' gives a wrong order, you need a better key.
up
3
brettz9 throu gh yah
18 years ago
Often, one may have a group of arrays which have parallel data that need to be kept associated with each other (e.g., the various attribute values of a group of elements might be stored in their own arrays). Using array_multisort as is, by specifying additional fields, it is possible, as in the documentation example cited below, that this association will be lost.

To take this example set of data from the documentation:
<?php
$ar1
= array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
?>

The example goes on to sort it this way:
<?php
array_multisort
($ar1, $ar2);
?>

In this case, although the "10" remains associated with the first '1' after being sorted, the "2" and '3' are reversed from their original order.

In order to sort by one field only (yet still have the other array(s) being correspondingly sorted), one can use array_keys (which makes an array out of the keys) to ensure that no further sub-sorting is performed. This works because array_keys is making an array for which no duplicates can exist (since keys will be unique), and thus, the subsequent fields will have no relevance as far as subsorting.

So, using the above data, we can perform this sort instead:
<?php
$ar3
= array_keys($ar1);
array_multisort($ar1, $ar3, $ar2);
?>

which, when $ar1 and $ar2 are dumped gives:

array(4) {
[0]=> string(2) "10"
[1]=> string(1) "a"
[2]=> int(100)
[3]=> int(100)
}
array(4) {
[0]=> int(1)
[1]=> int(1)
[2]=> int(3)
[3]=> string(1) "2"
}
To Top