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