I can not understand. Why this code remove last item. But if i change $item in second loop on $item2, or use link, it work fine.
<?php
$list = [
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
['id' => 5],
['id' => 6],
];
$selected = [2,3,4,6];
$hidden = [4,5];
foreach ($list as &$item) {
if(in_array($item['id'], $selected)) {
$item['selected'] = true;
}
}
foreach ($list as $key=>$item) {
if(in_array($item['id'], $hidden)) {
unset($list[$key]);
}
}
var_dump($list);
unset($item)after the firstforeachto break the association of$itemwith the last array element