PHP Shuffle () function to take a number of random elements of the array method

Source: Internet
Author: User
Tags shuffle
This article mainly introduces PHP function shuffle () take the array of several random elements of the method, combined with the case of the function of the shuffle function, definition, use of methods and related considerations, the need for friends can refer to the next

This paper describes the PHP function shuffle () to take the array of random elements of a method. Share to everyone for your reference, as follows:

Sometimes we need to take a number of random elements in the array (such as random recommendations), so how can PHP be implemented? A relatively simple workaround is to use PHP's own shuffle () function.

Shuffle ()

The shuffle () function rearranges the elements in the array in random order. Returns TRUE if successful, otherwise FALSE is returned. This function assigns the new key name to the cells in the array, which removes the existing key names and not just the reordering.

Here's a simple example:

$data [] = Array (    "name" = "Home of the Script", "rank" = "+"    ), $data [] = Array (    "name" = "Blog Park",    "rank" => ; $data [] = Array (    "name" = "CSDN",    "rank" = "()"), $data [] = Array (    "name" = "Iteye",    " Rank "+"), Shuffle ($data), $i = 0;foreach ($data as $key + $value) {    if ($i < 2) {        echo $data [$key] [Nam E ']. ' <br/> ';    }    $i + +;}

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.