How to Return a 2 dimentional array and get values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cyanide_Pierce
Forum Newbie
Posts: 5
Joined: Wed Nov 09, 2011 9:17 am

How to Return a 2 dimentional array and get values

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

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

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
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

Post by Cyanide_Pierce »

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

Post 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
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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');
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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
}
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

Post 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
Post Reply