Home  >  Article  >  php教程  >  PHP 反向排序和随机排序代码

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

WBOY
WBOYOriginal
2016-06-13 12:15:121311browse

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
Statement:
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