Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: array random

  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    60
    Thanks
    14
    Thanked 0 Times in 0 Posts

    array random

    Hi I have created an array for random allocation of holiday months

    <?php
    $months = array("Feburary", "April", "June");
    ?>

    does anyone know how i could randomly allocate one of the 3 specified months in every year over the period of 9 years

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    PHP Code:
    <?php
    $months 
    = array("Feburary""April""June");



    for(
    $i=0;$i<108;$i++)
    {
    echo 
    $i ": " $months[rand(0,(count($months)-1))] . "<br />";
    }  


    ?>
    Last edited by Adee; 11-20-2011 at 07:37 PM.

  • Users who have thanked Adee for this post:

    helen11 (11-20-2011)

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    60
    Thanks
    14
    Thanked 0 Times in 0 Posts
    How can i print the randomly allocated month in each year

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by helen11 View Post
    How can i print the randomly allocated month in each year
    well.. there are 108 months in 12 years so every 12 numbers it echos is a year..

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    This will return 9 arrays all consisting of 12 random months from your array

    PHP Code:
    $months = array("Feburary""April""June");


    $years = array();
    for(
    $i=1;$i<13;$i++)
    {
    for(
    $x=1;$x<10;$x++)
    {
        
    $years[$x][] = $months[rand(0,(count($months)-1))];
    }

    }  

    var_dump($years); 

  • Users who have thanked Adee for this post:

    helen11 (11-21-2011)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •