Analysis of the method of PHP function shuffle to get several random elements from an array shuffle academy shuffle how to download songs shuffle memorie

WBOY
Release: 2016-07-29 08:54:42
Original
1195 people have browsed it

The example in this article describes how the PHP function shuffle() takes several random elements from an array. Share it with everyone for your reference, the details are as follows:

Sometimes we need to take several random elements in the array (such as making random recommendation functions), so how to implement it in PHP? A relatively simple solution is to use the shuffle() function that comes with PHP. Here is a simple example:

$data[] = array(
    "name" => "本站",
    "rank" => "40"
);
$data[] = array(
    "name" => "博客园",
    "rank" => "50"
);
$data[] = array(
    "name" => "CSDN",
    "rank" => "60"
);
$data[] = array(
    "name" => "ITEYE",
    "rank" => "50"
);
shuffle($data);
$i = 0;
foreach($data as $key =>$value ){
    if($i < 2) {
        echo $data[$key]['name'].'<br />';
    }
    $i++;
}

Copy after login

shuffle()

shuffle() function rearranges the elements in the array in random order. Returns TRUE if successful, FALSE otherwise. This function assigns new keys to the elements in the array. This will delete the original keys rather than just reorder them.

If you pass in an associative array, you will find that the key names of the associative array will be lost. By the way, let’s talk about the solution here:

// 打乱关联数组的排序
function shuffle_assoc($array)
{ 
  $randomized_keys = array_rand($array, count($array)); 
  foreach($randomized_keys as $current_key)
  { 
    $output[$current_key] = $array[$current_key]; 
  } 
  return $output;
}

Copy after login

In addition, PHP also provides a function to randomly extract values ​​from an array: array_rand(). Its calling format is as follows: array_rand (array, the number of extracted elements); Of course, it must be implemented Support for two-dimensional arrays also needs to be encapsulated before it can be used. But the previous program supports two-dimensional arrays very well.

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax tutorial", "PHP operating office document skills" Summary (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php+mysql database operation introduction" Tutorial" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

The above introduces the analysis of the PHP function shuffle to get several random elements from the array, including the contents of shuffle and php. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!