PHP的sort函數(shù)可以用來對數(shù)組進(jìn)行排序。下面是一些正確使用sort函數(shù)進(jìn)行排序的示例:
$numbers = array(4, 2, 8, 6);
sort($numbers);
print_r($numbers);
$fruits = array("apple", "orange", "banana", "kiwi");
sort($fruits);
print_r($fruits);
$ages = array("Peter"=>32, "John"=>28, "Mary"=>35);
asort($ages);
print_r($ages);
$ages = array("Peter"=>32, "John"=>28, "Mary"=>35);
ksort($ages);
print_r($ages);
請注意,sort函數(shù)會直接修改原始數(shù)組,如果不想修改原始數(shù)組,可以先對數(shù)組進(jìn)行復(fù)制再進(jìn)行排序。