Home > Backend Development > PHP Tutorial > Analysis of how PHP function shuffle() takes several random elements from an array, shuffle array_PHP tutorial

Analysis of how PHP function shuffle() takes several random elements from an array, shuffle array_PHP tutorial

WBOY
Release: 2016-07-12 08:55:29
Original
841 people have browsed it

Analysis of the method of PHP function shuffle() to get several random elements from an array, shuffle array

The example in this article describes the method of PHP function shuffle() to get 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 PHP's own shuffle() function. Here’s 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()

The 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 an associative array is passed in, you will find that the key names of the associative array will be lost. By the way, here’s the solution:

// 打乱关联数组的排序
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 for randomly extracting values ​​from an array: array_rand(). Its calling format is as follows: array_rand (array, the number of extracted elements); Of course, to support two-dimensional arrays, encapsulation is also required. It can be used immediately. 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 on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax", "php operation office documentation" Summary of skills (including word, excel, access, ppt)", "Summary of php date and time usage", "Introduction to php object-oriented programming tutorial", "Summary of php string (string) usage", "Introduction to php mysql database operation" Tutorial" and "Summary of Common PHP Database Operation Skills"

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

Articles you may be interested in:

  • Usage of shuffle array value random sorting function in PHP
  • Instance of implementing PHP's shuffled array function shuffle in JavaScript
  • PHP array function sequence shuffle() and array_rand() Introduction to the use of random functions
  • PHP array function array_multisort() usage example analysis
  • Summary of common PHP array function usage
  • Summary of php array function array_key_exists()
  • Use the array function in PHP to create a new array
  • php Use the array_slice function to obtain a random array or the first few pieces of data
  • php uses the array_search function to implement Array search method
  • Summary of PHP array and explode function examples
  • PHP uses in_array function to check whether a certain value exists in the array
  • Summary of PHP array related functions
  • php uses the gettimeofday function to return the current time and store it in the associative array
  • php function end() usage to access the last element of the array

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117037.htmlTechArticleAnalysis of the method of PHP function shuffle() to take several random elements from an array, shuffle array This article describes the PHP function shuffle with examples () Method to get several random elements from an array. Share it with everyone...
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