A simple example of random values for PHP arrays

Source: Internet
Author: User
This article mainly introduces the PHP array random value of a simple example, has a certain reference value, now share to everyone, the need for friends can refer to

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

Array_rand--random extraction of one or more cells from an array

Mixed Array_rand (array input [, int num_req])

Array_rand () is useful when you want to remove one or more random cells from an array. It accepts input as an input array and an optional parameter, Num_req, indicating how many units you want to remove-if not specified, the default is 1.
If you only take one, Array_rand () returns the key name of a random cell, or returns an array containing the random key name. This allows you to randomly remove the key names and values from the array.
Do not forget to call Srand () to scatter the seed 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 PHP to achieve this function.

Steps

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

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

Srand (float) microtime () * 10000000);

After that 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 ability to randomly select an element from an array:

$rn = Array_rand ($image);

Then we'll show 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 the image of the code, if we want to make each picture with the respective connection address then we will 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 the 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 '  '; 

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.