'Filter array of associative rows to retain only rows with unique value in specified column [duplicate]
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?
Solution 1:[1]
I think this can be done without looping. Let me show an example:
$rows = array (
array ('id' => 1, 'code' => '342'),
array ('id' => 2, 'code' => '765'),
array ('id' => 3, 'code' => '134'),
array ('id' => 1, 'code' => '342')
);
$input = array_map("unserialize", array_unique(array_map("serialize", $rows)));
echo '<pre>';
print_r($input);
Output:
Array
(
[0] => Array
(
[id] => 1
[code] => 342
)
[1] => Array
(
[id] => 2
[code] => 765
)
[2] => Array
(
[id] => 3
[code] => 134
)
)
Solution 2:[2]
I want to remove one of them (doesn't matter which).
Okay, use array_column() to assign temporary first level keys using the id values. Because PHP will not allow two elements in the same level to share the same key, each subsequent duplicate will overwrite the earlier encountered row. Wrap that call in array_values() to remove the temporary keys (re-index the array).
Code: (Demo)
var_export(
array_values(array_column($rows, null, 'id'))
);
Output:
array (
0 =>
array (
'id' => 1,
'code' => '999',
),
1 =>
array (
'id' => 2,
'code' => '765',
),
2 =>
array (
'id' => 3,
'code' => '134',
),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Object Manipulator |
| Solution 2 |
