Today, I just added a random generation article function to the PHP blog, using array_rand in the PHP function.
Today, I just added a random generation article function to the PHP blog. 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 selectedReturns an array containing a random key name.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;