Page 1 of 1

How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 9:20 am
by Cyanide_Pierce
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

Re: How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 9:28 am
by Celauran
Two problems I see; your function doesn't return anything, and $april, $may, and $august aren't defined.

Re: How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 10:21 am
by Cyanide_Pierce
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

Re: How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 10:24 am
by Celauran

Code: Select all

return $foo;
inside your function.

For the month variables, you'll either need to declare them as global or pass them into the function as arguments.

Re: How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 10:25 am
by Cyanide_Pierce
months are now gloabal but still dont know how to send the array back and assign the array

Re: How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 10:40 am
by Cyanide_Pierce
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
}

Re: How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 10:40 am
by Celauran

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

Posted: Wed Nov 09, 2011 10:42 am
by Celauran

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
}

Re: How to Return a 2 dimentional array and get values

Posted: Wed Nov 09, 2011 10:47 am
by Cyanide_Pierce
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