PHP function Shuffle An analysis of several random elements in an array shuffle Gakuen shuffle how to sing shuffle Memorie

Source: Internet
Author: User
Tags getting started with php php programming shuffle
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. Here's a simple example:

$data [] = Array (    "name" = "Site", "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 ']. '
'; } $i + +;}

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.

If you pass in an associative array, you will find that the key name of the associated array is missing. By the way, here's the solution:

The Sort function Shuffle_assoc ($array) {   $randomized _keys = Array_rand ($array, Count ($array)) that disrupts the associative array;   foreach ($randomized _keys as $current _key)  {     $output [$current _key] = $array [$current _key];   }   return $output;}

In addition, PHP provides a function to randomly extract values from an array: Array_rand (), whose invocation format is as follows: Array_rand (array, number of extracted elements); Of course, to implement support for two-dimensional arrays needs to be encapsulated before they can be used. But the previous program is good for supporting two-dimensional arrays.

More readers interested in PHP related content can view this site: "Summary of PHP operations and operator usage," PHP Network Programming Tips Summary, "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document tips summary (including word,excel,access, ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

The above describes the PHP function shuffle array of several random elements of the method analysis, including the shuffle,php aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.