1

I have an array and can't really narrow down the right way to print everything out.

Array:

$month = Array(
    "1" => "January",
    "2" => "February",
    "3" => "March",
    "4" => "April",
    "5" => "May",
    "6" => "June",
    "7" => "July",
    "8" => "August",
    "9" => "September",
    "10" => "October",
    "11" => "November",
    "12" => "December"
);

What I Tried:

function monthList() {
    require "config.php";
        echo '<select name="month" title="Choose A Month">';
    foreach ($month as $i => $value) {
        echo '<option value="'.the_month_#_here.'">'.$month[$i].'</option>';
    }
        echo '</select>';
}

Which works fine for pulling out the name but how would i pull out the number associated with it in the array? Thats what I need as the option value.

2
  • 4
    The number is $i from your foreach loop. Commented Jul 25, 2012 at 22:05
  • 1
    @rackemup420 And don't use $month[$i]. The point of the foreach loop is that you assigned each iteration to $value. Commented Jul 25, 2012 at 22:07

3 Answers 3

3
function monthList() {
    require "config.php";
        echo '<select name="month" title="Choose A Month">';
    foreach ($month as $i => $value) {
        echo '<option value="'.$i.'">'.$value.'</option>';
    }
        echo '</select>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Use $value in the foreach loop, not $month[$i].
@Michael, that is much better form, yes, but either should work.
1

Use

echo '<option value="'.$i.'">'.$value.'</option>';

instead of

echo '<option value="'.the_month_#_here.'">'.$month[$i].'</option>';

Comments

0

Try this Hope this helps :).

   function monthList() {
        require "config.php";
            echo '<select name="month" title="Choose A Month">';
        foreach ($month as $i => $value) {
            echo '<option value="'.$i.'">'.$value.'</option>';
        }
            echo '</select>';
    }

Best of luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.