So what I have is an array of things. The items in the array are put into a drop down with the tag, nothing new or complicated about that but here’s the (current working) code anyway:
<?php foreach($roles as $role): ?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php endforeach ?>
Now what needs to happen, is that if the $role[‘name’] is “Basic”, it should not be displayed. I have been trying (and googling) for this, and I have been unsuccessful. I didn’t think it was that big of a deal.
Here’s what I’m trying:
<?php foreach($roles as $role): ?>
<?php if(!$role['name'] == "Basic") { ?>
<option value="<?php echo $role['id']; ?>"><?php echo $role['name']; ?></option>
<?php } ?>
<?php endforeach ?>
When I try that it doesn’t add any fields to the drop down at all, so I’m obviously missing something here. Any tips would be appreciated, thanks!
Your problem is with operator precedence [docs]:
Assuming that
role['name']is never empty or contains the string"0"(see converting to boolean), this will be evaluated aswhich is always
false. Use!=instead or write!(role['name'] == "Basic").You can use the alternative style for the
ifstatement too: