您好,登錄后才能下訂單哦!
怎么在php中對數(shù)組進(jìn)行隨機(jī)取值?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
array_rand() 在你想從數(shù)組中取出一個或多個隨機(jī)的單元時相當(dāng)有用。它接受 input 作為輸入數(shù)組和一個可選的參數(shù) num_req,指明了你想取出多少個單元 - 如果沒有指定,默認(rèn)為 1。
array_rand -- 從數(shù)組中隨機(jī)取出一個或多個單元
mixed array_rand ( array input [, int num_req])
array_rand() 在你想從數(shù)組中取出一個或多個隨機(jī)的單元時相當(dāng)有用。它接受 input 作為輸入數(shù)組和一個可選的參數(shù) num_req,指明了你想取出多少個單元 - 如果沒有指定,默認(rèn)為 1。
如果你只取出一個,array_rand() 返回一個隨機(jī)單元的鍵名,否則就返回一個包含隨機(jī)鍵名的數(shù)組。這樣你就可以隨機(jī)從數(shù)組中取出鍵名和值。
不要忘記調(diào)用 srand() 來撒下隨機(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";
我們曾經(jīng)訪問過這樣的網(wǎng)站,每次刷新banner都隨機(jī)的變化,在這篇文章中,我們將給大家介紹用PHP來實(shí)現(xiàn)這個功能。
步驟
程序?qū)崿F(xiàn)的原理是:調(diào)用一個數(shù)組,每個圖象對應(yīng)一個數(shù)組中的元素,然后我們設(shè)置隨機(jī)數(shù),只要隨機(jī)得到一個數(shù)據(jù)就可以顯示一副圖象了。
第一個步是我們來產(chǎn)生一個隨機(jī)數(shù)。每次刷新時我們都得到不同的隨機(jī)數(shù),具體代碼為:
srand((float) microtime() * 10000000);
之后我們設(shè)置一個數(shù)組為image,然后再設(shè)置5個數(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ī)選擇一個元素:
$rn = array_rand($image);
然后我們來顯示一個隨機(jī)的圖片:
echo '<img src="'.$image[$rn].'">';
把上面的代碼組合起來就可以了。
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ī)顯示圖片的代碼,如果我們想使每個圖片再加上各自的連接地址那么我們把上述的代碼稍微改動下就可以了!把上述的數(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'].'">';
關(guān)于怎么在php中對數(shù)組進(jìn)行隨機(jī)取值問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。