php 数组函数

【asort】排序数组(返回值保留原键名)

asort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool

sort 函数的唯一区别是:返回值中保留键名。

第二个参数效果完全等同于 sort,请参考

https://qianjinyike.com/【sort】排序数组(不再保留键名)

<?php
$fruits = [
    "d" => "lemon",
    "a" => "orange",
    "b" => "banana",
    "c" => "apple",
];
asort($fruits);

foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}

/*
c = apple
b = banana
d = lemon
a = orange
*/

发表评论