Simple Example of random php array values, php array values

Source: Internet
Author: User

Simple Example of random php array values, php array values

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

The simple example of the above php array random value is all the content shared by xiaobian. I hope you can give us a reference and support the house of friends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.