A simple example of PHP array random value _php tips

Source: Internet
Author: User
Tags rand

Array_rand () is useful when you want to take one or more random units out of an array. It accepts input as an input array and an optional parameter num_req, indicating how many units you want to take out-if not specified, the default is 1.

Array_rand--to randomly remove one or more cells from an array

Mixed Array_rand (array input [, int num_req]) 

Array_rand () is useful when you want to take one or more random units out of an array. It accepts input as an input array and an optional parameter num_req, indicating how many units you want to take out-if not specified, the default is 1.

If you only remove one, Array_rand () returns the key name of a random cell, otherwise it returns an array containing the random key name. This allows you to randomly retrieve the key names and values from the array.

Don't forget to call Srand () to scatter 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 have visited such a site, each refresh banner random changes, in this article, we will introduce you to use PHP to achieve this function.

Steps

The principle of program implementation is: Call an array, each image corresponds to an array of elements, and then we set the random number, as long as a random to get a data can display an image.

The first step is for us to produce a random number. Each time we refresh, we get a different random number, the specific code is:

Srand ((float) microtime () * 10000000); 

Then we set an array to image and then set 5 elements of the array, 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 implements the function of randomly selecting an element from an array:

$rn = Array_rand ($image); 

And then we'll show you a random picture:

Echo '  '; 

Put the above code together.

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 our random display of pictures of the code, if we want to make each picture plus their respective connection address so we have to change the above code slightly! Change the above 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 the function of our title, randomly display the picture and connect to a different specified address:

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 code above to your Web page to run it. Good luck

The above PHP array random value of a simple example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.