亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php 數(shù)組隨機(jī)取值的簡(jiǎn)單實(shí)例

 更新時(shí)間:2016年05月23日 11:11:17   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇php 數(shù)組隨機(jī)取值的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧

array_rand() 在你想從數(shù)組中取出一個(gè)或多個(gè)隨機(jī)的單元時(shí)相當(dāng)有用。它接受 input 作為輸入數(shù)組和一個(gè)可選的參數(shù) num_req,指明了你想取出多少個(gè)單元 - 如果沒有指定,默認(rèn)為 1。

array_rand -- 從數(shù)組中隨機(jī)取出一個(gè)或多個(gè)單元

mixed array_rand ( array input [, int num_req]) 

array_rand() 在你想從數(shù)組中取出一個(gè)或多個(gè)隨機(jī)的單元時(shí)相當(dāng)有用。它接受 input 作為輸入數(shù)組和一個(gè)可選的參數(shù) num_req,指明了你想取出多少個(gè)單元 - 如果沒有指定,默認(rèn)為 1。

如果你只取出一個(gè),array_rand() 返回一個(gè)隨機(jī)單元的鍵名,否則就返回一個(gè)包含隨機(jī)鍵名的數(shù)組。這樣你就可以隨機(jī)從數(shù)組中取出鍵名和值。

不要忘記調(diào)用 srand() 來(lái)撒下隨機(jī)數(shù)發(fā)生器的種子。

例子 1. array_rand() 例子

srand ((float) microtime() * 10000000); 
$input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); 
$rand_keys = array_rand ($input, 2); 
print $input[$rand_keys[0]]."\n"; 
print $input[$rand_keys[1]]."\n"; 

我們?cè)?jīng)訪問過這樣的網(wǎng)站,每次刷新banner都隨機(jī)的變化,在這篇文章中,我們將給大家介紹用PHP來(lái)實(shí)現(xiàn)這個(gè)功能。

步驟

程序?qū)崿F(xiàn)的原理是:調(diào)用一個(gè)數(shù)組,每個(gè)圖象對(duì)應(yīng)一個(gè)數(shù)組中的元素,然后我們?cè)O(shè)置隨機(jī)數(shù),只要隨機(jī)得到一個(gè)數(shù)據(jù)就可以顯示一副圖象了。

第一個(gè)步是我們來(lái)產(chǎn)生一個(gè)隨機(jī)數(shù)。每次刷新時(shí)我們都得到不同的隨機(jī)數(shù),具體代碼為:

srand((float) microtime() * 10000000); 

之后我們?cè)O(shè)置一個(gè)數(shù)組為image,然后再設(shè)置5個(gè)數(shù)組元素,代碼如下:

$image[1]='/location/of/image1.jpg'; 
$image[2]='/location/of/image2.jpg'; 
$image[3]='/location/of/image3.jpg'; 
$image[4]='/location/of/image4.jpg'; 
$image[5]='/location/of/image5.jpg'; 

下面的代碼實(shí)現(xiàn)的功能是從數(shù)組中隨機(jī)選擇一個(gè)元素:

$rn = array_rand($image); 

然后我們來(lái)顯示一個(gè)隨機(jī)的圖片:

echo '<img src="'.$image[$rn].'">'; 

把上面的代碼組合起來(lái)就可以了。

srand((float) microtime() * 10000000); 
$image[1]='/location/of/image1.jpg'; 
$image[2]='/location/of/image2.jpg'; 
$image[3]='/location/of/image3.jpg'; 
$image[4]='/location/of/image4.jpg'; 
$image[5]='/location/of/image5.jpg'; 
$rn = array_rand($image); 
echo '<img src="'.$image[$rn].'">'; 

以上的代碼是我們隨機(jī)顯示圖片的代碼,如果我們想使每個(gè)圖片再加上各自的連接地址那么我們把上述的代碼稍微改動(dòng)下就可以了!把上述的數(shù)組改為二維數(shù)組:

$image[1]['pic']='/location/of/image1.jpg'; 
$image[1]['link']='/location/of/link1.php'; 

相應(yīng)的顯示代碼為:

echo '<a href="'.$image[$rn]['link'].'">'; 
echo '<img src="'.$image[$rn]['pic'].'">'; 

那么我們就可以完成我們標(biāo)題的功能了,隨機(jī)顯示圖片并且連接到不同的指定的地址:

srand((float) microtime() * 10000000); 
$image[1]['pic']='/location/of/image1.jpg'; 
$image[1]['link']='/location/of/link1.php'; 
$image[2]['pic']='/location/of/image2.jpg'; 
$image[2]['link']='/location/of/link2.php'; 
$image[3]['pic']='/location/of/image3.jpg'; 
$image[3]['link']='/location/of/link3.php'; 
$image[4]['pic']='/location/of/image4.jpg'; 
$image[4]['link']='/location/of/link4.php'; 
$image[5]['pic']='/location/of/image5.jpg'; 
$image[5]['link']='/location/of/link5.php'; 
$rn = array_rand($image); 
echo '<a href="'.$image[$rn]['link'].'">'; 
echo '<img src="'.$image[$rn]['pic'].'">'; 

你可以把上面的代碼拷到你的網(wǎng)頁(yè)中去運(yùn)行了。祝你好運(yùn)

以上這篇php 數(shù)組隨機(jī)取值的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論