array_multisort

(PHP 4, PHP 5, PHP 7)

array_multisort锟皆讹拷锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

说锟斤拷

array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] ) : bool

array_multisort() 锟斤拷锟斤拷锟斤拷锟斤拷一锟轿对讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟津,伙拷锟竭革拷锟斤拷某一维锟斤拷锟轿拷远锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷string锟斤拷锟斤拷锟斤拷锟斤拷锟街诧拷锟戒,锟斤拷锟斤拷锟街硷拷锟斤拷锟结被锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

锟斤拷锟斤拷

array1

要锟斤拷锟斤拷锟� array锟斤拷

array1_sort_order

之前 array 锟斤拷锟斤拷要锟斤拷锟叫碉拷顺锟斤拷 SORT_ASC 锟斤拷锟斤拷锟斤拷锟斤拷顺锟斤拷锟斤拷锟斤拷 SORT_DESC 锟斤拷锟斤拷锟铰斤拷顺锟斤拷锟斤拷锟斤拷

锟剿诧拷锟斤拷锟斤拷锟皆猴拷 array1_sort_flags 锟斤拷锟斤拷锟斤拷也锟斤拷锟斤拷锟斤拷全删锟斤拷锟斤拷默锟斤拷锟斤拷 SORT_ASC 锟斤拷

array1_sort_flags

array 锟斤拷锟斤拷锟借定选锟筋:

锟斤拷锟斤拷锟斤拷锟酵憋拷志锟斤拷

  • SORT_REGULAR - 锟斤拷锟斤拷目锟斤拷锟斤拷通锟斤拷锟斤拷锟斤拷锟饺较o拷锟斤拷锟睫革拷锟斤拷锟酵o拷
  • SORT_NUMERIC - 锟斤拷锟斤拷锟斤拷锟街达拷小锟饺斤拷
  • SORT_STRING - 锟斤拷锟斤拷锟街凤拷锟斤拷锟饺斤拷
  • SORT_LOCALE_STRING - 锟斤拷锟捷碉拷前锟侥憋拷锟截伙拷锟斤拷锟矫o拷锟斤拷锟斤拷锟街凤拷锟斤拷锟饺较★拷 锟斤拷锟斤拷使锟斤拷 locale 锟斤拷息锟斤拷锟斤拷锟斤拷通锟斤拷 setlocale() 锟睫改达拷锟斤拷息锟斤拷
  • SORT_NATURAL - 锟斤拷锟街凤拷锟斤拷锟斤拷"锟斤拷然锟斤拷锟斤拷"锟斤拷锟斤拷锟斤拷 natsort()
  • SORT_FLAG_CASE - 锟斤拷锟斤拷锟斤拷锟� (锟斤拷位锟斤拷 OR) SORT_STRING 锟斤拷锟斤拷 SORT_NATURAL 锟斤拷小写锟斤拷锟斤拷锟叫的凤拷式锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟皆猴拷 array1_sort_order 锟斤拷锟斤拷锟斤拷锟斤拷省锟皆o拷默锟斤拷锟斤拷锟斤拷锟斤拷锟� SORT_REGULAR锟斤拷

...

锟斤拷选锟斤拷选锟筋,锟斤拷锟结供锟斤拷锟斤拷锟斤拷锟介,锟斤拷锟斤拷锟斤拷 sort order 锟斤拷 sort flag 之锟斤拷 锟结供锟斤拷锟斤拷锟斤拷锟街帮拷锟斤拷锟斤拷锟揭拷锟斤拷锟酵拷锟斤拷锟斤拷锟皆拷亍锟� 锟斤拷锟斤拷之锟斤拷锟斤拷锟斤拷锟角帮拷锟街碉拷顺锟斤拷锟斤拷锟叫的★拷

锟斤拷锟斤拷值

锟缴癸拷时锟斤拷锟斤拷 TRUE锟斤拷 锟斤拷锟斤拷锟斤拷失锟斤拷时锟斤拷锟斤拷 FALSE锟斤拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.4.0 array1_sort_flags 锟斤拷锟斤拷 SORT_NATURAL 锟斤拷 SORT_FLAG_CASE 选锟筋。
5.3.0 array1_sort_flags 锟斤拷锟斤拷选锟斤拷 SORT_LOCALE_STRING锟斤拷

锟斤拷锟斤拷

Example #1 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�

<?php
$ar1 
= array(101001000);
$ar2 = array(1324);
array_multisort($ar1$ar2);

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

锟斤拷锟斤拷锟斤拷锟斤拷铮拷锟斤拷锟襟,碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 0锟斤拷 10锟斤拷 100锟斤拷 100锟斤拷 锟节讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 4锟斤拷1锟斤拷 2锟斤拷 3锟斤拷 锟节讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥匡拷锟接︼拷锟揭伙拷锟斤拷锟斤拷锟斤拷也锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷100 锟斤拷 100锟斤拷锟斤拷

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

Example #2 锟斤拷锟斤拷锟轿拷锟斤拷锟�

<?php
$ar 
= array(
       array(
"10"11100100"a"),
       array(   
1,  2"2",   3,   1)
      );
array_multisort($ar[0], SORT_ASCSORT_STRING,
                
$ar[1], SORT_NUMERICSORT_DESC);
var_dump($ar);
?>

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷螅锟揭伙拷锟斤拷锟斤拷榻拷锟斤拷 "10"锟斤拷100锟斤拷100锟斤拷11锟斤拷"a"锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫o拷锟斤拷锟节讹拷锟斤拷锟斤拷锟介将锟斤拷锟斤拷 1, 3, "2", 2, 1锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆斤拷锟斤拷锟斤拷锟叫o拷锟斤拷

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

Example #3 锟斤拷锟斤拷锟捷匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷 data 锟斤拷锟斤拷锟叫碉拷每锟斤拷锟斤拷元锟斤拷示一锟斤拷锟斤拷锟叫碉拷一锟叫★拷锟斤拷锟角碉拷锟酵碉拷锟斤拷锟捷匡拷锟铰硷拷锟斤拷锟斤拷菁锟斤拷稀锟�

锟斤拷锟斤拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟铰o拷

volume | edition
-------+--------
    67 |       2
    86 |       1
    85 |       6
    98 |       2
    86 |       6
    67 |       7

锟斤拷锟斤拷全锟斤拷锟斤拷锟斤拷锟斤拷锟轿� data 锟斤拷锟斤拷锟斤拷锟叫★拷锟斤拷通锟斤拷锟斤拷通锟斤拷循锟斤拷锟斤拷锟斤拷锟捷匡拷取锟矫的斤拷锟斤拷锟斤拷锟斤拷锟� mysql_fetch_assoc()锟斤拷

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

锟斤拷锟斤拷锟叫斤拷锟斤拷 volume 锟斤拷锟斤拷锟斤拷锟叫o拷锟斤拷 edition 锟斤拷锟斤拷锟斤拷锟叫★拷

锟斤拷锟斤拷锟斤拷锟剿帮拷锟斤拷锟斤拷锟叫碉拷锟斤拷锟介,锟斤拷锟斤拷 array_multisort() 锟斤拷要一锟斤拷锟斤拷锟斤拷锟叫碉拷锟斤拷锟介,锟斤拷锟斤拷锟斤拷锟斤拷麓锟斤拷锟斤拷锟饺★拷锟斤拷校锟饺伙拷锟斤拷锟斤拷锟�

<?php
// 取锟斤拷锟叫碉拷锟叫憋拷
foreach ($data as $key => $row) {
    
$volume[$key]  = $row['volume'];
    
$edition[$key] = $row['edition'];
}

// 锟斤拷锟斤拷锟捷革拷锟斤拷 volume 锟斤拷锟斤拷锟斤拷锟叫o拷锟斤拷锟斤拷 edition 锟斤拷锟斤拷锟斤拷锟斤拷
// 锟斤拷 $data 锟斤拷为锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟酵拷眉锟斤拷锟斤拷锟�
array_multisort($volumeSORT_DESC$editionSORT_ASC$data);
?>

锟斤拷锟捷硷拷锟斤拷锟斤拷锟斤拷锟脚猴拷锟斤拷锟剿o拷锟斤拷锟斤拷锟斤拷拢锟�

volume | edition
-------+--------
    98 |       2
    86 |       1
    86 |       6
    85 |       6
    67 |       2
    67 |       7

Example #4 锟斤拷锟斤拷锟街达拷小写锟斤拷母锟斤拷锟斤拷

SORT_STRING 锟斤拷 SORT_REGULAR 锟斤拷锟斤拷锟斤拷锟街达拷小写锟斤拷母锟侥o拷锟斤拷写锟斤拷母锟斤拷锟斤拷锟斤拷小写锟斤拷母之前锟斤拷

要锟斤拷锟叫诧拷锟斤拷锟街达拷小写锟斤拷锟斤拷锟津,撅拷要锟斤拷锟斤拷原锟斤拷锟斤拷锟叫⌒达拷锟侥革拷锟斤拷锟斤拷锟斤拷锟斤拷锟�

<?php
$array 
= array('Alpha''atomic''Beta''bank');
$array_lowercase array_map('strtolower'$array);

array_multisort($array_lowercaseSORT_ASCSORT_STRING$array);

print_r($array);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

锟轿硷拷