Home > php教程 > php手册 > body text

PHP 反向排序和随机排序代码

WBOY
Release: 2016-06-13 12:15:12
Original
1310 people have browsed it

array_reverse()函数与shuffle()函数介绍

array_reverse()
array array_reverse(array)array_reverse()函数传入参数为一数组,返回一个与传入参数值相同但顺序相反的数组。

复制代码 代码如下:


$a = array(1,2,3,4,5);
$a = array_reverse($a);
for ($i=0; $iecho $a[$i]." ";
?>


结果为:

5 4 3 2 1

shuffle()
bool shuffle(array)shuffle函数对传入的数组进行随机排序,成功返回TRUE,否则返回FALSE。

复制代码 代码如下:


$a = array(1,2,3,4,5);
shuffle($a);
for ($i=0; $iecho $a[$i]." ";
shuffle($a);
echo "
";
for ($i=0; $iecho $a[$i]." ";
?>


两次调用返回的结果:

4 1 2 5 3
1 5 2 4 3
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!