我正在从官方手册中学习php,刚刚学习了Arrays部分的示例#13
https://www.php.net/manual/en/language.types.array.php
当我从命令行使用php localserver在本地windows 10中运行示例代码时
sort()
实际上是不排序数组。我尝试了以下代码:
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
print_r($files);
sort($files);
print_r($files);
closedir($handle);
?>
我得到的输出如下:
Array
(
[0] => .
[1] => ..
[2] => .ftpquota
[3] => Ftp fxg710ehhrpx.xml
[4] => index.html
[5] => index.php
[6] => Logo
[7] => myphp
[8] => OnlineSlap.rar
)
Array
(
[0] => .
[1] => ..
[2] => .ftpquota
[3] => Ftp fxg710ehhrpx.xml
[4] => Logo
[5] => OnlineSlap.rar
[6] => index.html
[7] => index.php
[8] => myphp
)
如你所见,在使用之前
sort
数组按字母顺序排列,但在
排序()
顺序变得随机。
为什么阵列
unsorted
预期的行为是什么?
谢谢您。