Development encountered a refresh page random access to the article function, the PHP function in the Array_rand () function, Array_rand (array,number), there are two parameters, where the array parameter is to represent what you want to get a certain number of groups, numbers optional parameters, The second parameter is used to determine the number of elements to select, the default is 1. If more than one element is selected, the array containing the random key name is returned, otherwise the key name of the element is returned. (Remember: To understand the meaning of the bold part), the return is an array key name, not the corresponding value in the array, I just started to think that it is directly return a numeric value, the old do not get the value. My specific usage is this:
PHP code
- . .......
- .......
- while ($res =mysql_fetch_object ($rsql))
- {
- $arr [$i][id]= $res->art_id;
- $arr [$i][url]= SiteURL.] art-php-"." $res->art_id ". html
- $arr [$i][title]= $res->art_title;
- $i;
- }
- $arr 1=array_rand ($arr, 10);
- $arr 2=array ();
- foreach ($arr 1 as $key = $value)
- {
- $arr 2[$key][id]= $arr [$value][id];
- $arr 2[$key][url]= $arr [$value][url];
- $arr 2[$key][title]= $arr [$value][title];
- }
- return $arr 2;
Here's an explanation of the code I wrote:
$arr represents the array that I have obtained, $arr 1 returns a random 10 array key name, and then re-combines the array with the return key via foreach.
http://www.bkjia.com/PHPjc/486131.html www.bkjia.com true http://www.bkjia.com/PHPjc/486131.html techarticle Development encountered a refresh page random access to the article function, the PHP function in the Array_rand () function, Array_rand (array,number), there are two parameters, where the array parameter is to represent what you want ...