Filter/Remove rows where column value is found more than once in a multidimensional array

Viewed 435

I need to remove rows from my input array where duplicate values occur in a specific column.

Sample array:

$array = [
    ['user_id' => 82, 'ac_type' => 1],
    ['user_id' => 80, 'ac_type' => 5],
    ['user_id' => 76, 'ac_type' => 1],
    ['user_id' => 82, 'ac_type' => 1],
    ['user_id' => 80, 'ac_type' => 5]
];

I'd like to filter by user_id to ensure uniqueness and achieve this result:

So, my output will be like this:

[
    ['user_id' => 82, 'ac_type' => 1],
    ['user_id' => 80, 'ac_type' => 5],
    ['user_id' => 76, 'ac_type' => 1]
]

I've already tried with:

$result = array_unique($array, SORT_REGULAR);

and

$result = array_map("unserialize", array_unique(array_map("serialize", $array)));

and

$result = array();
foreach ($array as $k => $v) {
    $results[implode($v)] = $v;
}
$results = array_values($results);
print_r($results);

but duplicate rows still exist.

4 Answers
Related