質問

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)

正しい解決策はありません

他のヒント

Instead of iterating as a key/value pair, try iterating with just the element itself and checking the individual indexes:

foreach ($row as $task) {
    $value = ($task['Status'] == '0') ? '<del>' . $task['Task'] . '</del>' : $task['Task'];
    echo '<td>' . $value . '</td>';
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top