php function Shuffle () take an array of several random elements of the method analysis, shuffle array _php tutorial

Source: Internet
Author: User
Tags getting started with php shuffle

php function Shuffle () takes an array of several random elements of the method analysis, shuffle array

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" = "Home for help", "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.

Articles you may be interested in:

    • Shuffle array values in PHP random sort function usage
    • Shuffle instance of PHP's scrambled array function implemented in JavaScript
    • Introduction to the use of shuffle () and Array_rand () random functions in the sequence of PHP array functions
    • PHP Array function array_multisort () Usage Example analysis
    • Summary of common array function usage in PHP
    • PHP Array function array_key_exists () Summary
    • Create a new array using the array function in PHP
    • PHP uses the Array_slice function to get a random array or the first few data
    • PHP uses Array_search function to implement array lookup method
    • Example Summary of PHP arrays and explode functions
    • PHP uses the In_array function to check if a value exists in the array
    • PHP Array Correlation Function Summary
    • PHP uses the Gettimeofday function to return the current time and store it in an associative array.
    • PHP access to the last element of the array function end () usage true techarticle php function Shuffle () take the array of several random elements of the method analysis, shuffle array in this paper, the PHP function shuffle () take the array of several random elements of the method. Share for the big ...

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