PHP的sort函數用于對數組進行升序排序。使用方法如下:
$fruits = array("apple", "orange", "banana", "pear");
sort($fruits);
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
執行以上代碼后,輸出結果為:
apple
banana
orange
pear
如果想要對數組進行降序排序,可以使用rsort函數。示例代碼如下:
$fruits = array("apple", "orange", "banana", "pear");
rsort($fruits);
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
執行以上代碼后,輸出結果為:
pear
orange
banana
apple