Home > Backend Development > PHP Tutorial > PHP array_rand() function of random article function_PHP tutorial

PHP array_rand() function of random article function_PHP tutorial

WBOY
Release: 2016-07-13 17:41:36
Original
1316 people have browsed it

During development, I encountered a function to refresh the page to randomly obtain articles. I used the array_rand() function in the PHP function, array_rand(array, number), which has two parameters. The array parameter represents an array you want to obtain. Number can be Select parameters. The second parameter is used to determine how many elements to select. The default is 1. . If more than one element is selected, an array containing a random key is returned, otherwise the key of the element is returned. (Remember: understand the meaning of the bold part). What is returned is an array key name, not the corresponding value in the array. When I first started using it, I thought it was returning a numerical value directly, but I couldn't get the value. My specific usage is as follows:
PHP code

  1. . .....
  2. ..... ....
  3. while($res=mysql_fetch_object($rsql))
  4. {
  5. $arr[$i][id]=$res->art_id;
  6. $arr[ $i][url]= siteurl."art-php-"."$res->art_id"..html; ;
  7. $i ;
  8. } }
  9. $arr1=array_rand($arr,10);
  10. $arr2=array();
  11. foreach($arr1 as $key=>$value)
  12. {
  13. $arr2[$key][id]=$arr[$value][id];
  14. $arr2[$key][url]=$arr[$value][url];
  15. $arr2[$key][title]=$arr[$value][title] ;
  16. }                                                                                                                                                                              For the array I have obtained, $arr1 returns 10 random array key names, and then uses foreach to recombine the array based on the returned keys.
  17. http://www.bkjia.com/PHPjc/486131.html
  18. www.bkjia.com
true

http: //www.bkjia.com/PHPjc/486131.htmlTechArticle

During development, I encountered the function of refreshing the page to randomly obtain articles. I used the array_rand() function in the PHP function, array_rand (array,number), has two parameters, the array parameter represents what you want...
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