多维数组的排序
函数原型是 bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $... ]]] )
array_multisort() 函数可以对多个PHP数组进行排序
第一个参数是要排序的数组,第二个及其后的参数是其他数组 或者 排序的规则
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
result is
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)
}
after sorting, the first array will contain 0, 10, 100, 100.
The second array will contain 4, 1, 2, 3.
The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.
注意
两个数组的元素个数必须相同,不然就会出现一个警告信息:
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in ……
$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);
?>
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).
$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);
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
SORT_ASC – 按照上升顺序排序
SORT_DESC – 按照下降顺序排序
SORT_REGULAR – 将项目按照通常方法比较
SORT_NUMERIC – 将项目按照数值比较
SORT_STRING – 将项目按照字符串比较
每个数组之后不能指定两个同类的排序标志
每个数组后指定的排序标志仅对该数组有效
在此之前为默认值 SORT_ASC 和 SORT_REGULAR。
这个函数改变数字索引,其他索引不变!
The dataset is now sorted, and will look like this:
volume | edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
Case insensitive sorting
Both SORT_STRING and SORT_REGULAR are case sensitive, strings starting with a capital letter will come before strings starting with a lowercase letter.
To perform a case insensitive search, force the sorting order to be determined by a lowercase copy of the original array.
$array = array('Alpha', 'atomic', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
print_r($array);
?>
The above example will output:
Array
(
[0] => Alpha
[1] => atomic
[2] => bank
[3] => Beta
)
以下是官方 英文描述
array_multisort(PHP 4, PHP 5)
array_multisort — Sort multiple or multi-dimensional arrays
bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $... ]]] )
array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.
Associative (string) keys will be maintained, but numeric keys will be re-indexed.
Report a bug Parameters
arr
An array being sorted.
arg
Optionally another array, or sort options for the previous array argument: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING.
Additional arg's.
Report a bug Return Values
Returns TRUE on success or FALSE on failure.
函数原型是 bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $... ]]] )
array_multisort() 函数可以对多个PHP数组进行排序
第一个参数是要排序的数组,第二个及其后的参数是其他数组 或者 排序的规则
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
result is
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)
}
after sorting, the first array will contain 0, 10, 100, 100.
The second array will contain 4, 1, 2, 3.
The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.
注意
两个数组的元素个数必须相同,不然就会出现一个警告信息:
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in ……
$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);
?>
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).
$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);
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
SORT_ASC – 按照上升顺序排序
SORT_DESC – 按照下降顺序排序
SORT_REGULAR – 将项目按照通常方法比较
SORT_NUMERIC – 将项目按照数值比较
SORT_STRING – 将项目按照字符串比较
每个数组之后不能指定两个同类的排序标志
每个数组后指定的排序标志仅对该数组有效
在此之前为默认值 SORT_ASC 和 SORT_REGULAR。
这个函数改变数字索引,其他索引不变!
The dataset is now sorted, and will look like this:
volume | edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
Case insensitive sorting
Both SORT_STRING and SORT_REGULAR are case sensitive, strings starting with a capital letter will come before strings starting with a lowercase letter.
To perform a case insensitive search, force the sorting order to be determined by a lowercase copy of the original array.
$array = array('Alpha', 'atomic', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
print_r($array);
?>
The above example will output:
Array
(
[0] => Alpha
[1] => atomic
[2] => bank
[3] => Beta
)
以下是官方 英文描述
array_multisort(PHP 4, PHP 5)
array_multisort — Sort multiple or multi-dimensional arrays
bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $... ]]] )
array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.
Associative (string) keys will be maintained, but numeric keys will be re-indexed.
Report a bug Parameters
arr
An array being sorted.
arg
Optionally another array, or sort options for the previous array argument: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING.
Additional arg's.
Report a bug Return Values
Returns TRUE on success or FALSE on failure.
12朵
1个