php function Shuffle () the method of taking several random elements of an array to analyze _php techniques

Source: Internet
Author: User
Tags shuffle

This article illustrates the method of shuffle () of PHP function to take several random elements of an array. Share to everyone for your reference, specific as follows:

Sometimes we need to take a few random elements of the array (like doing random recommendation), so how does PHP work? A simpler solution is to use PHP's own shuffle () function. Here's a simple example:

$data [] = Array (
    "name" => "cloud-dwelling community",
    "Rank" => "
)";
$data [] = Array (
    "name" => "blog Park",
    "Rank" => ")"
;
$data [] = Array (
    "name" => "CSDN",
    "Rank" => ")"
;
$data [] = Array (
    "name" => "Iteye", "
    rank" => "M"
);
Shuffle ($data);
$i = 0;
foreach ($data as $key => $value) {
    if ($i < 2) {
        echo $data [$key] [' name ']. ' <br/> ';
    }
    $i + +;
}

Shuffle ()

The shuffle () function rearranges the elements in the array in random order. If successful, returns TRUE, otherwise returns FALSE. This function assigns a new key name to the cell in the array, which deletes the original key name and not just the reordering.

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

Disrupting the sort function of associative arrays
Shuffle_assoc ($array)
{ 
  $randomized _keys = Array_rand ($array, Count ($array)); 
  foreach ($randomized _keys as $current _key)
  { 
    $output [$current _key] = $array [$current _key]; 
  } 
  return $output;
}

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

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.