PHP | asort()函数
asort()函数是PHP中的一个内置函数,用于根据值对数组进行排序。它以保持索引和值之间的关系的方式进行排序。默认情况下,它按值的升序排序。
句法:
bool asort( $array, $sorting_type )
参数:该函数接受上面提到的两个参数,如下所述:
- $array:此参数指定要排序的数组。它是一个强制参数。
- $sorting_type:这是一个可选参数。下面讨论了不同的排序类型:
- SORT_REGULAR: $sorting_type的值是 SORT_REGULAR 然后项目正常比较。
- SORT_NUMERIC: $sorting_type的值是 SORT_NUMERIC 然后项目是数字比较。
- SORT_STRING: $sorting_type的值是 SORT_STRING 然后项目被比较为字符串。
- SORT_LOCALE_STRING: $sorting_type的值为 SORT_STRING,然后根据当前语言环境将项目作为字符串进行比较。
返回值:此函数在成功时返回 True,在失败时返回 False。
下面的程序说明了PHP中的 asort()函数。
方案一:
"Web Technology",
"1" => "Machine Learing",
"2" => "GeeksforGeeks",
"3" => "Computer Graphics",
"4" => "Videos",
"5" => "Report Bug",
"6" => "Article",
"7" => "Sudo Placement",
"8" => "SContribute",
"9" => "Reset",
"10" => "Copy",
"11" => "IDE",
"12" => "Gate Note",
);
// Implementation of asort()
asort($arr);
// for-Loop for displaying result
foreach ($arr as $key => $val) {
echo "[$key] = $val";
echo"\n";
}
?>
输出:
[6] = Article
[3] = Computer Graphics
[10] = Copy
[12] = Gate Note
[2] = GeeksforGeeks
[11] = IDE
[1] = Machine Learing
[5] = Report Bug
[9] = Reset
[8] = SContribute
[7] = Sudo Placement
[4] = Videos
[0] = Web Technology
方案二:
11,
"b" => 22,
"d" => 33,
"n" => 44,
"o" => 55,
"p" => 66,
"r" => 77,
"s" => 2,
"q" => -11,
"t" => 3,
"u" => 1000,
"z" => 1,
);
// Implementation of asort()
asort($arr);
// for-Loop for displaying result
foreach ($arr as $key => $val) {
echo "[$key] = $val";
echo"\n";
}
?>
输出:
[q] = -11
[z] = 1
[s] = 2
[t] = 3
[a] = 11
[b] = 22
[d] = 33
[n] = 44
[o] = 55
[p] = 66
[r] = 77
[u] = 1000
相关文章:
- 排序()函数
- arsort()函数
- uksort()函数
- usort()函数
参考: 函数 : PHP 。 PHP