我在描述中没有找到的第一件事是,此函数返回的结果从最小值到最大值,例如:[-5=>'', 0=>'', 5=>'' ]
您还应该知道,默认情况下,它对作为字符串表示但值为数字的键具有正确的排序,例如:['-5'=>'', '0'=>'', '5'=>'' ]
几个带有结果的示例
-----------------------------------------
描述:键是数字 + 默认标志 (SORT_REGULAR)
$arr = [
-5 => 'minus five',
0 => 'zero',
1 => 'one',
2 => 'two',
100 => 'hundred',
];
ksort($arr);
print_r($arr);
结果
数组
(
[-5] => minus five
[0] => zero
[1] => one
[2] => two
[100] => hundred
)
-----------------------------------------
描述:键是字符串数字 + 默认标志 (SORT_REGULAR)
$arr = [
'-5' => 'minus five',
'0' => 'zero',
'1' => 'one',
'2' => 'two',
'100' => 'hundred',
];
ksort($arr);
print_r($arr);
结果
数组
(
[-5] => minus five
[0] => zero
[1] => one
[2] => two
[100] => hundred
)
-----------------------------------------
描述:键是字符串数字 + SORT_STRING 标志
$arr = [
'-5' => 'minus five',
'0' => 'zero',
'1' => 'one',
'2' => 'two',
'100' => 'hundred',
];
ksort($arr, SORT_STRING);
print_r($arr);
结果
数组
(
[-5] => minus five
[0] => zero
[1] => one
[100] => hundred
[2] => two
)
-----------------------------------------
描述:键是字符串数字 + SORT_NUMERIC 标志
$arr = [
'-5' => 'minus five',
'0' => 'zero',
'1' => 'one',
'2' => 'two',
'100' => 'hundred',
];
ksort($arr, SORT_NUMERIC);
print_r($arr);
结果
数组
(
[-5] => minus five
[0] => zero
[1] => one
[2] => two
[100] => hundred
)