Loop through Array with conditional output based on key/value pair
Posted
by
Daniel C
on Stack Overflow
See other posts from Stack Overflow
or by Daniel C
Published on 2012-11-04T02:20:19Z
Indexed on
2012/11/04
5:00 UTC
Read the original article
Hit count: 149
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)
© Stack Overflow or respective owner