SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    for($i 1$i <= $years$i++)
    {
    $month rand(13);
    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

    PHP Code:
    function setMonth()
    {
    $month rand(13);
    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
    Last edited by SpacePhoenix; Nov 9, 2011 at 13:05. Reason: put php tags around php code

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Either global $holis or return it.
    Pass $i as a parameter.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

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