Random now article function of PHP Array_rand () function _php Tutorial

Source: Internet
Author: User
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
    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. $arr [$i][title]= $res->art_title;
    8. $i;
    9. }
    10. $arr 1=array_rand ($arr, 10);
    11. $arr 2=array ();
    12. foreach ($arr 1 as $key = $value)
    13. {
    14. $arr 2[$key][id]= $arr [$value][id];
    15. $arr 2[$key][url]= $arr [$value][url];
    16. $arr 2[$key][title]= $arr [$value][title];
    17. }
    18. 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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.