数组排序时发现 sort 与 rsort 排序完毕以后,var_dump 输出时原有 key 值丢失,变为数字索引。
<?php $a=array("key1"=>"val1","key6"=>"val6","key3"=>"val1","key5"=>"val6","key4"=>"val8"); echo "sort(\$a)=".PHP_EOL; sort($a); var_dump($a); ?>
输出如下:
sort($a)= array(5) { [0]=> string(4) "val1" [1]=> string(4) "val1" [2]=> string(4) "val6" [3]=> string(4) "val6" [4]=> string(4) "val8" }
<?php $a=array("key1"=>"val1","key6"=>"val6","key3"=>"val1","key5"=>"val6","key4"=>"val8"); echo "rsort(\$a)=".PHP_EOL; rsort($a); var_dump($a); ?>
输出如下:
rsort($a)= array(5) { [0]=> string(4) "val8" [1]=> string(4) "val6" [2]=> string(4) "val6" [3]=> string(4) "val1" [4]=> string(4) "val1" }