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

php shuffle()对数组随机排序代码

WBOY
Release: 2016-06-13 10:42:44
Original
968 people have browsed it

shuffle() 函数把数组中的元素按随机顺序重新排列。

若成功,则返回 TRUE,否则返回 FALSE。

注释:本函数为数组中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。

注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已被自动完成

$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
print_r($my_array);
?>
下面看一个// 用数组存放广告列表

$ads = array(广告 1
,广告 2
,广告 3
,广告 4
);

// 对数组进行随机排序
shuffle($ads);

// 输出经过排序的数组
$html = ;
foreach ($ads as $ad) {
$html .= $ad;
}
echo $html;

 
 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template