Loop through Array with conditional output based on key/value pair
- by Daniel C
I have an array with the following columns:
Task
Status
I would like to print out a table that shows a list of the Tasks, but not the Status column. Instead, for Tasks where the Status = 0, I want to add a tag <del> to make the completed task be crossed out. Here's my current code:
foreach ($row as $key => $val){
if ($key != 'Status') print "<td>$val</td>";
else if ($val == '0') print "<td><del>$val</del></td>";
}
This seems to be correct, but when I print it out, it prints all the tasks with the <del> tag. So basically the "else" clause is being run every time.
Here is the var_dump($row):
array
'Task' => string 'Task A' (length=6)
'Status' => string '3' (length=1)
array
'Task' => string 'Task B' (length=6)
'Status' => string '0' (length=1)