How to Return a 2 dimentional array and get values
Moderator: General Moderators
-
Cyanide_Pierce
- Forum Newbie
- Posts: 5
- Joined: Wed Nov 09, 2011 9:17 am
How to Return a 2 dimentional array and get values
Im using the following code quite often and wish to take some of the code and put it into a function and call the funtion and still output the array data.
This is what i have:
for($i = 1; $i <= $years; $i++)
{
$month = rand(1, 3);
if($month == 1)
{
$holis[$i][0] = "April";
$holis[$i][1] = $april;
}
elseif($month == 2)
{
$holis[$i][0] = "May";
$holis[$i][1] = $may;
}
elseif($month == 3)
{
$holis[$i][0] = "August";
$holis[$i][1] = $august;
}
$total = $total + $holis[$i][1];
echo "Year $i holidays are in {$holis[$i][0]} and you get {$holis[$i][1]} days worth of holidays.<br />\n\t";
}
I wish to make it something like this, use a functon to set and check the month before outputing the data
function setMonth()
{
$month = rand(1, 3);
if($month == 1)
{
$holis[$i][0] = "April";
$holis[$i][1] = $april;
}
elseif($month == 2)
{
$holis[$i][0] = "May";
$holis[$i][1] = $may;
}
elseif($month == 3)
{
$holis[$i][0] = "August";
$holis[$i][1] = $august;
}
}
if($option == 1)
{
echo "<p>\n\t";
for($i = 1; $i <= $years; $i++)
{
setMonth();
$total = $total + $holis[$i][1];
echo "Year $i holidays are in {$holis[$i][0]} and you get {$holis[$i][1]} days worth of holidays.<br />\n\t";
}
}
Ive tried different things to get setMonth() to output the data
This is what i have:
for($i = 1; $i <= $years; $i++)
{
$month = rand(1, 3);
if($month == 1)
{
$holis[$i][0] = "April";
$holis[$i][1] = $april;
}
elseif($month == 2)
{
$holis[$i][0] = "May";
$holis[$i][1] = $may;
}
elseif($month == 3)
{
$holis[$i][0] = "August";
$holis[$i][1] = $august;
}
$total = $total + $holis[$i][1];
echo "Year $i holidays are in {$holis[$i][0]} and you get {$holis[$i][1]} days worth of holidays.<br />\n\t";
}
I wish to make it something like this, use a functon to set and check the month before outputing the data
function setMonth()
{
$month = rand(1, 3);
if($month == 1)
{
$holis[$i][0] = "April";
$holis[$i][1] = $april;
}
elseif($month == 2)
{
$holis[$i][0] = "May";
$holis[$i][1] = $may;
}
elseif($month == 3)
{
$holis[$i][0] = "August";
$holis[$i][1] = $august;
}
}
if($option == 1)
{
echo "<p>\n\t";
for($i = 1; $i <= $years; $i++)
{
setMonth();
$total = $total + $holis[$i][1];
echo "Year $i holidays are in {$holis[$i][0]} and you get {$holis[$i][1]} days worth of holidays.<br />\n\t";
}
}
Ive tried different things to get setMonth() to output the data
Re: How to Return a 2 dimentional array and get values
Two problems I see; your function doesn't return anything, and $april, $may, and $august aren't defined.
-
Cyanide_Pierce
- Forum Newbie
- Posts: 5
- Joined: Wed Nov 09, 2011 9:17 am
Re: How to Return a 2 dimentional array and get values
The months are defined outside the for.
But how do i return stuff? does return($holis); work for the array? and if so how do i grab them in the for loop
But how do i return stuff? does return($holis); work for the array? and if so how do i grab them in the for loop
Re: How to Return a 2 dimentional array and get values
Code: Select all
return $foo;For the month variables, you'll either need to declare them as global or pass them into the function as arguments.
-
Cyanide_Pierce
- Forum Newbie
- Posts: 5
- Joined: Wed Nov 09, 2011 9:17 am
Re: How to Return a 2 dimentional array and get values
months are now gloabal but still dont know how to send the array back and assign the array
-
Cyanide_Pierce
- Forum Newbie
- Posts: 5
- Joined: Wed Nov 09, 2011 9:17 am
Re: How to Return a 2 dimentional array and get values
i have that but how do i get it to work in the for loop now?
if($option == 1){
echo "<p>\n\t";
for($i = 1; $i <= $years; $i++){
$holis = setMonth(); <---- HERE IS WHERE IM HAVING THE PROBLEMS
$total = $total + $holis[$i][1];
echo "Year $i holidays are in {$holis[$i][0]} and you get {$holis[$i][1]} days worth of holidays.<br />\n\t";
}
echo "<br />Over $years years you will get $total days holidays\n</p>\n";
// END OPTION 1
}
if($option == 1){
echo "<p>\n\t";
for($i = 1; $i <= $years; $i++){
$holis = setMonth(); <---- HERE IS WHERE IM HAVING THE PROBLEMS
$total = $total + $holis[$i][1];
echo "Year $i holidays are in {$holis[$i][0]} and you get {$holis[$i][1]} days worth of holidays.<br />\n\t";
}
echo "<br />Over $years years you will get $total days holidays\n</p>\n";
// END OPTION 1
}
Re: How to Return a 2 dimentional array and get values
Code: Select all
function setMonth($year)
{
global $april, $may, $august;
$month = rand(1, 3);
if ($month == 1)
{
$holis[$year][0] = "April";
$holis[$year][1] = $april;
}
elseif ($month == 2)
{
$holis[$year][0] = "May";
$holis[$year][1] = $may;
}
elseif ($month == 3)
{
$holis[$year][0] = "August";
$holis[$year][1] = $august;
}
return $holis;
}
$foo = setMonth('2009');Re: How to Return a 2 dimentional array and get values
Code: Select all
function setMonth($year)
{
global $april, $may, $august;
$month = rand(1, 3);
if ($month == 1)
{
$holis[$year][0] = "April";
$holis[$year][1] = $april;
}
elseif ($month == 2)
{
$holis[$year][0] = "May";
$holis[$year][1] = $may;
}
elseif ($month == 3)
{
$holis[$year][0] = "August";
$holis[$year][1] = $august;
}
return $holis;
}
if ($option == 1)
{
echo "<p>\n\t";
for ($i = 1; $i <= $years; $i++)
{
$holis = setMonth($i);
$total = $total + $holis[$i][1];
echo "Year $i holidays are in {$holis[$i][0]} and you get {$holis[$i][1]} days worth of holidays.\n\t";
}
echo "Over $years years you will get $total days holidays\n</p>\n";
// END OPTION 1
}-
Cyanide_Pierce
- Forum Newbie
- Posts: 5
- Joined: Wed Nov 09, 2011 9:17 am
Re: How to Return a 2 dimentional array and get values
Thank you got it working. I needed to pass the $i through for counting each year. The $years values was where the loop had to stop
Thanks againg
Thanks againg