2

I'm trying to output certain values of this array. It's a months/season array. The user is asked to input a season, and depending on what season they input, I want it to output months in that season.

Here is what I have so far -

$user_input = $_POST ['user_input'];

$month_season = array(
    'January' => 'Winter',
    'February' => 'Spring',
    'March' => 'Spring',
    'April' => 'Spring',
    'May' => 'Summer',
    'June' => 'Summer',
    'July' => 'Summer',
    'August' => 'Autumn',
    'September' => 'Autumn',
    'October' => 'Autumn',
    'November' => 'Winter',
    'December' => 'Winter'
);

$j = 0;
foreach ($month_season as $value) {
    if ($month_season[$j] = 'Winter')
    {
        echo $month_season[$j];
        echo "<br>";
        $j++;
    }
}

For example, i'm just using winter until i get it working, then i will expand the code for all seasons. However, when I input winter in the previous page, the out put is 12 winters. I assume it's just outputting winter for each of the iterations.

Why is this? What should I be doing here to get what I want?

Thanks in advance.

5
  • 2
    1) If you use foreach then you don't need a counter variable foreach ($month_season as $value) 2) = assignment ; == comparison (Also just to note here: You can't use associative keys as numerical keys) Commented Feb 27, 2016 at 13:58
  • if ($month_season[$j] = 'Winter') should be like if ($value ==$user_input). Commented Feb 27, 2016 at 14:02
  • If you do: print_r($month_season); after your code you will see what you created. Commented Feb 27, 2016 at 14:02
  • So it should be more like this - foreach ($month_season as $value) { if ($month_season[ ? ] == $user_input) { echo $month_season[ ? ]; echo "<br>"; } } I've put in the question marks in the array index as i'm still confused as to how to check if that array value is "winter" and if it is, how to output just that value? Thanks! Commented Feb 27, 2016 at 14:20
  • @D_isforPaul You want to use the foreach: php.net/manual/en/control-structures.foreach.php Commented Feb 27, 2016 at 15:09

2 Answers 2

1

When you are comparing values in PHP, you must use either == or === operator. When using = in if statement, you give your $month_season[$j] the value of Winter.

I think this is a more elegant solution:

$seasonArray = array(
            'January' => 'Winter',
            'February' => 'Spring',
            'March' => 'Spring',
            'April' => 'Spring',
            'May' => 'Summer',
            'June' => 'Summer',
            'July' => 'Summer',
            'August' => 'Autumn',
            'September' => 'Autumn',
            'October' => 'Autumn',
            'November' => 'Winter',
            'December' => 'Winter'
            );

$monthArray = array();

while(array_search('Winter', $seasonArray) !== FALSE) {
    $key = array_search('Winter', $seasonArray);
    $monthArray[] = $key;
    unset($seasonArray[$key]);
}

print_r($monthArray);

In the end $monthArray holds the needed values:

Array
(
    [0] => January
    [1] => November
    [2] => December
)
Sign up to request clarification or add additional context in comments.

Comments

1

You could also use a different array-Structure just like

$seasons = array (
    'Spring' => array('February', 'March', 'April' ),
    'Summer' => array('May', 'June', 'July' ),
    'Autumn' => array('August', 'September', 'October' ),
    'Winter' => array('January', 'November', 'December' ),
)

echo $seasons[$user_input];

1 Comment

This seems like a nice way to do what i'm trying. I was trying multidimensional arrays earlier too. Trying this I get an error though - even when i put in echo $seasons['Winter']; i still get the error. The error is - Notice: Array to String conversion

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.