Php array random value-small_123

Source: Internet
Author: User
The random value of the php array-small_123 array_rand () is useful when you want to extract one or more random units from the 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 '';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 '';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.

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.