I have the following array:
$array = [
['id' => 1, 'code' => '342'],
['id' => 2, 'code' => '765'],
['id' => 3, 'code' => '134'],
['id' => 1, 'code' => '999'],
];
Here Array[0]['id']
and Array[3]['id']
are duplicates with respect to ['id']. I want to remove one of them (doesn't matter which).
Actually I thought I found a solution with this code:
//data
$row = $stmt->fetchALL(PDO::FETCH_ASSOC);
$ids = array();
for($i = 0;$i < count($row); $i++ )
{
if (in_array($row[$i]['id'], $ids))
{
unset($row[$i]);
continue;
}
$ids[] = $row[$i]['id'];
}
print_r($row);
For some reason it works well with small arrays, but if I have a lot of values in it with multiple duplicates, it fails. Any suggestion to what I am missing?