我正在从官方手册中学习 php,刚刚在数组部分https://www.php.net/manual/en/language.types.array.php上的示例 #13当我在本地 Windows 10 中运行示例代码时使用命令行中的 php localserver 我观察到sort()实际上对数组进行了排序。我尝试了以下代码:
<?php
// 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数组之前是按字母顺序排列的,但在使用之后sort()顺序就变得随机了。
为什么数组得到了unsorted,排序的预期行为是什么?
谢谢你。