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
- . .....
- ..... ....
- while($res=mysql_fetch_object($rsql))
- {
- $arr[$i][id]=$res->art_id;
- $arr[ $i][url]= siteurl."art-php-"."$res->art_id"..html; ;
- $i ;
- } }
- $arr1=array_rand($arr,10);
- $arr2=array();
- foreach($arr1 as $key=>$value)
- {
- $arr2[$key][id]=$arr[$value][id];
- $arr2[$key][url]=$arr[$value][url];
- $arr2[$key][title]=$arr[$value][title] ;
- } 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.
-
- http://www.bkjia.com/PHPjc/486131.html
- 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...
Statement: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