PHP array Random value Simple instance, php array value _php tutorial

Source: Internet
Author: User

Simple example of random value of PHP array, PHP array value


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

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

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:

After that we set an array to image and then set 5 elements of the array, the code is as follows:

The following code implements the ability to randomly select an element from an array:

Then we'll show a random picture:

Put the above code together.

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:

The corresponding display code is:

Then we can complete the function of our title, randomly display the picture and connect to the different specified address:

You can copy the above code to your Web page and run it. Good luck

Above this PHP array random value of a simple example is the small part to share the whole content of everyone, I hope to give you a reference, but also hope that we support a lot of help the home.

http://www.bkjia.com/PHPjc/1133095.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133095.html techarticle A simple example of random values for PHP arrays, the PHP array value Array_rand () is useful when you want to remove one or more random cells from an array. It accepts input as an array of inputs and a ...

  • Related Article

    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.