PHP的sort函数用于对数组进行升序排序。下面是sort函数的使用方法和技巧:
$numbers = array(4, 2, 8, 6);
sort($numbers);
function compareLength($a, $b) {
return strlen($a) - strlen($b);
}
$fruits = array("apple", "banana", "orange");
usort($fruits, "compareLength");
$ages = array("Peter" => 32, "John" => 28, "Mary" => 30);
asort($ages);
$users = array(
array("name" => "John", "age" => 28),
array("name" => "Mary", "age" => 30),
array("name" => "Peter", "age" => 32)
);
usort($users, function($a, $b) {
return $a['age'] - $b['age'];
});
$numbers = array(4, 2, 8, 6);
rsort($numbers);
以上是sort函数的使用方法和技巧,可以根据实际需求选择合适的方法来对数组进行排序。