Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2011, 07:03 PM   PM User | #1
helen11
New Coder

 
Join Date: Nov 2011
Location: South Africa
Posts: 46
Thanks: 12
Thanked 0 Times in 0 Posts
helen11 is an unknown quantity at this point
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
helen11 is offline   Reply With Quote
Old 11-20-2011, 07:34 PM   PM User | #2
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
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..
Adee is offline   Reply With Quote
Users who have thanked Adee for this post:
helen11 (11-20-2011)
Old 11-20-2011, 08:02 PM   PM User | #3
helen11
New Coder

 
Join Date: Nov 2011
Location: South Africa
Posts: 46
Thanks: 12
Thanked 0 Times in 0 Posts
helen11 is an unknown quantity at this point
How can i print the randomly allocated month in each year
helen11 is offline   Reply With Quote
Old 11-20-2011, 08:18 PM   PM User | #4
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
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..
Adee is offline   Reply With Quote
Old 11-20-2011, 08:22 PM   PM User | #5
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
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); 
Adee is offline   Reply With Quote
Users who have thanked Adee for this post:
helen11 (11-21-2011)
Reply

Bookmarks

Tags
array, php, random

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:31 PM.


Advertisement
Log in to turn off these ads.