I have an array full of associative arrays like so:
$arr = [
['title' => 'My title', 'content' => 'lorem ...', comments: 'lorem ipsum'],
['title' => 'My title 2', 'content' => 'lorem ...'],
['title' => 'My title 3', 'content' => 'lorem ...'],
['title' => 'My title 4', 'content' => 'lorem ...', comments: 'lorem ipsum'],
];
As you can see, some of them don't have comments.
The problem is, I have then a foreach loop like this:
<?php foreach($arr as $key => $value){
extract($value);
?>
<div>
...etc
<?php if($comment): ?>
<span><?= $comment ?></span>
<?php endif; ?>
</div>
<?php } ?>
In the second iteration, the variable $comments now holds the value of the first item in the array, cause it doesn't find the property in the associative array and it uses the last one, breaking the if statement.
Is there any way to avoid this without having to add a comments: null or something to the original array?
if (isset($value['comment'])) { ... }extract(). You don't need it. Ever.extract(), when ̶c̶o̶r̶r̶e̶c̶t̶l̶y̶ used, leads quickly to a code impossible to maintain, and therefore, grants a lifetime job