php array_multisort() multi-array sorting method_PHP tutorial

WBOY
Release: 2016-07-13 17:13:34
Original
895 people have browsed it

In php, array_multisort() can sort multiple arrays at one time, or sort multi-dimensional arrays according to a certain dimension or multiple dimensions. It returns TRUE if successful and FALSE if failed.

bool array_multisort (array ar1 [, mixed arg [, mixed ... [, array ...]]] )

Returns TRUE if successful, FALSE if failed.

array_multisort() can be used to sort multiple arrays at once, or to sort multi-dimensional arrays according to one or more dimensions.

Associative (string) key names remain unchanged, but numeric key names will be re-indexed.


Example 1. Sort multidimensional array

The code is as follows Copy code
 代码如下 复制代码

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

$ar = array(

array("10", 11, 100, 100, "a"),
代码如下 复制代码

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

array( 1, 2, "2", 3, 1)

);

array_multisort($ar[0], SORT_ASC, SORT_STRING,

                    $ar[1], SORT_NUMERIC, SORT_DESC);

var_dump($ar);
 代码如下 复制代码

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

?> In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2 ", 2, 1 (sorted as numbers, in descending order).
The code is as follows Copy code
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) } }
After sorting in this example, the first array will contain 10, 100, 100, "a" (as ascending order of strings), and the second array will contain 1, 3, "2", 1 (as numerically descending order). Example 2. Sorting multi-dimensional array
The code is as follows Copy code
$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);<🎜> ?>

In this example, after sorting, the first array will become "10", 100, 100, 11, "a" (treated as strings in ascending order). The second array will contain 1, 3, "2", 2, 1 (treated as numbers in descending order).

 代码如下 复制代码
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 Let’s take a comprehensive look at an example commonly used in applications.

 代码如下 复制代码

header('Content-Type: text/html; charset=utf-8');
echo '
'; 
//原始数组格式
$array = array(
'key1' => array(
'item1' => '65',
'item2' => '35',
'item3' => '84',
),
'key2' => array(
'item1' => '24',
),
'key3' => array(
'item1' => '38',
'item3' => '45',
),
);
//要排序的键
//按照数组中的 item1进行排序
//你也可以换成item2
$sort = 'item1';
foreach($array as $k => $v)
{
$newArr[$k] = $v[$sort];
}
//这个函数如果执行正确他会直接改变原数组键值的顺序
//如果执行失败,那么他会返回 bool(false)
array_multisort($newArr,SORT_DESC, $array);
var_dump($array);
//---------------------排序后的数组打印效果 开始--------------------
array(3) {
["key1"]=>
array(3) {
["item1"]=>
string(2) "65"
["item2"]=>
string(2) "35"
["item3"]=>
string(2) "84"
}
["key3"]=>
array(2) {
["item1"]=>
string(2) "38"
["item3"]=>
string(2) "45"
}
["key2"]=>
array(1) {
["item1"]=>
string(2) "24"
}
}
//---------------------排序后的数组打印效果 结束---------------------

For a detailed explanation of the array_multisort() function, please refer to http://www.bKjia.c0m/phper/php-function/39192.htm

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629150.htmlTechArticleIn php, array_multisort() can sort multiple arrays at one time, or sort them according to a certain dimension or multiple dimensions. Sort multidimensional arrays, returning TRUE if successful, and FALSE if failed. ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!