巧用rsort()和sort(),求PHP数组最大值和最小值
一、巧用rsort()求数组最大值
<?php
$fruits = array("155::武侠::http://www.movie618.com/movie/618133551.html", "1::电影618::http://www.movie618.com/", "2::窃听风云2::http://www.movie618.com/movie/618141596.html", "3::关云长::http://www.movie618.com/movie/618123753.html");
print_r($fruits);
rsort($fruits,SORT_NUMERIC);//降序排列,SORT_NUMERIC - 把值作为数字来处理,详见rsort()的用法
print_r($fruits);
?>
显示结果:
原数组:
Array
(
[0] => 155::武侠::http://www.movie618.com/movie/618133551.html
[1] => 1::电影618::http://www.movie618.com/
[2] => 2::窃听风云2::http://www.movie618.com/movie/618141596.html
[3] => 3::关云长::http://www.movie618.com/movie/618123753.html
)
排序后数组:
Array
(
[0] => 155::武侠::http://www.movie618.com/movie/618133551.html
[1] => 3::关云长::http://www.movie618.com/movie/618123753.html
[2] => 2::窃听风云2::http://www.movie618.com/movie/618141596.html
[3] => 1::电影618::http://www.movie618.com/
)
所以数组的第一个元素就是最大值。
二、巧用rsort()求数组最大值
<?php
$fruits = array("155::武侠::http://www.movie618.com/movie/618133551.html", "1::电影618::http://www.movie618.com/", "2::窃听风云2::http://www.movie618.com/movie/618141596.html", "3::关云长::http://www.movie618.com/movie/618123753.html");
print_r($fruits);
sort($fruits,SORT_NUMERIC);//升序排列,SORT_NUMERIC - 把值作为数字来处理,详见sort()的用法
print_r($fruits);
?>
查看 sort()的用法
显示结果:
原数组:
Array
(
[0] => 155::武侠::http://www.movie618.com/movie/618133551.html
[1] => 1::电影618::http://www.movie618.com/
[2] => 2::窃听风云2::http://www.movie618.com/movie/618141596.html
[3] => 3::关云长::http://www.movie618.com/movie/618123753.html
)
排序后数组:
Array
(
[0] => 1::电影618::http://www.movie618.com/
[1] => 2::窃听风云2::http://www.movie618.com/movie/618141596.html
[2] => 3::关云长::http://www.movie618.com/movie/618123753.html
[3] => 155::武侠::http://www.movie618.com/movie/618133551.html
)
所以数组的第一个元素就是最小值。