Php array random value, php array value
Array_rand () is useful when you want to extract one or more random units from an array. It accepts input as an input array and an optional parameter num_req, specifying the number of units you want to retrieve-if not specified, the default value is 1.
Array_rand -- randomly retrieve one or more units from the array
mixed array_rand ( array input [, int num_req])
Array_rand () is useful when you want to extract one or more random units from an array. It accepts input as an input array and an optional parameter num_req, specifying the number of units you want to retrieve-if not specified, the default value is 1.
If you only retrieve one key, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned. In this way, you can randomly retrieve the key name and value from the array.
Do not forget to call srand () to sow the seeds of the random number generator.
Example 1. array_rand () Example
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";
We once visited such a website and randomly changed every time we refreshed the banner. In this article, we will introduce you to using PHP to implement this function.
Procedure
Program implementation principle: Call an array, each image corresponds to an element in an array, then we set a random number, as long as a random data can be displayed a pair of images.
The first step is to generate a random number. Each time we refresh, we get different random numbers. The specific code is:
srand((float) microtime() * 10000000);
Then we set an array to image, and then set five array elements. The Code is as follows:
$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';
The following code randomly selects an element from the array:
$rn = array_rand($image);
Then we will display a random image:
echo '';
Combine the above Code.
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 '';
The above code is the code for randomly displaying images. If we want to add a connection address for each image, we can modify the above Code slightly! Change the preceding array to a two-dimensional array:
$image[1]['pic']='/location/of/image1.jpg';$image[1]['link']='/location/of/link1.php';
The corresponding display code is:
echo '<a href="'.$image[$rn]['link'].'">';echo '';
Then we can complete our title function, randomly display images and connect them to different specified addresses:
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 '';
You can copy the above Code to Your webpage to run it. Good luck
Address: http://www.manongjc.com/article/833.html
Related reading:
Php randomly retrieves several different numbers from an array.
Shuffle () and array_rand ()
Php randomly retrieves several values from the array.
Several Methods for php to randomly obtain/retrieve the value of an array
Several methods for generating multiple non-repeated random numbers using php
JavaScript to obtain random numbers within a specified range
Javascript obtains random numbers between m and n.