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函數的使用方法和技巧,可以根據實際需求選擇合適的方法來對數組進行排序。