php 數組隨機取值的簡單一實例_php技巧

來源:互聯網
上載者:User

array_rand() 在你想從數組中取出一個或多個隨機的單元時相當有用。它接受 input 作為輸入數組和一個可選的參數 num_req,指明了你想取出多少個單元 - 如果沒有指定,預設為 1。

array_rand -- 從數組中隨機取出一個或多個單元

mixed array_rand ( array input [, int num_req]) 

array_rand() 在你想從數組中取出一個或多個隨機的單元時相當有用。它接受 input 作為輸入數組和一個可選的參數 num_req,指明了你想取出多少個單元 - 如果沒有指定,預設為 1。

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

不要忘記調用 srand() 來撒下隨機數發生器的種子。

例子 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"; 

我們曾經訪問過這樣的網站,每次重新整理banner都隨機的變化,在這篇文章中,我們將給大家介紹用PHP來實現這個功能。

步驟

程式實現的原理是:調用一個數組,每個圖象對應一個數組中的元素,然後我們設定隨機數,只要隨機得到一個資料就可以顯示一副圖象了。

第一個步是我們來產生一個隨機數。每次重新整理時我們都得到不同的隨機數,具體代碼為:

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

之後我們設定一個數組為image,然後再設定5個數組元素,代碼如下:

$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].'">'; 

把上面的程式碼群組合起來就可以了。

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].'">'; 

以上的代碼是我們隨機顯示圖片的代碼,如果我們想使每個圖片再加上各自的串連地址那麼我們把上述的代碼稍微改動下就可以了!把上述的數組改為二維數組:

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

相應的顯示代碼為:

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

那麼我們就可以完成我們標題的功能了,隨機顯示圖片並且串連到不同的指定的地址:

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'].'">'; 

你可以把上面的代碼拷到你的網頁中去運行了。祝你好運

以上這篇php 數組隨機取值的簡單一實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.