During development, a page is refreshed and the article function is randomly obtained. The array_rand () function in the PHP function is used. array_rand (array, number) has two parameters, the array parameter indicates that you want to obtain an array. The number parameter is optional. The second parameter is used to determine the elements to be selected. The default value is 1 .. If more than one element is selected, an array containing the random key name is returned. Otherwise, the key name of the element is returned. (Remember: understand the meaning of bold), the returned is an array key name, not the corresponding value in the array. When I first started using it, I thought it was a direct return value, old values cannot be obtained. The 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;
- $ Arr [$ I] [title] = $ res-> art_title;
- $ 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];
- }
- Return $ arr2;
The code I wrote is as follows:
$ Arr indicates the array that I have obtained. $ arr1 returns 10 random array key names, and then reassembles the array with the return key through foreach.