在foreach循环中使用php if语句的问题

I did have a quick search on this but couldn't find anything relating to my problem. I'm having issues with an if statement within a foreach loop. I'm pulling a list of categories from one class, and a single category to be selected from another. The code below (with the if statement removed) works perfectly, and returns 12 options with populated values.

foreach ($user_info->categories as $key=>$category) {
    $category_name = $user_info->category_names[$key];
    echo '<option value="'.$category.'">'.$category_name.'</option>';
}

However when I add the if statement inside as below:

foreach ($user_info->categories as $key=>$category) {
    $category_name = $user_info->category_names[$key];
    if ($category = $get_article_info->category_1) {
        echo '<option value="'.$category.'" selected="selected">'.$category_name.'</option>';
    } else {
        echo '<option value="'.$category.'">'.$category_name.'</option>';
    }
}

I get a list of options with the text populated but all twelve options have value="". $get_article_info->category_1 works when echoed on it's own, and even if it were not found I would expect the if statement to echo 12 options without any selected (the same as the first code example).

You are using = instead of == You should change

 if ($category = $get_article_info->category_1) {

To

  if ($category == $get_article_info->category_1) {