As an example, I have the following array:
$groups = [
'group1' => [
'a' => 'able',
'b' => 'baker',
'd' => 'dog'
],
'group2' => [
'a' => 'able',
'c' => 'charlie',
'd' => 'dog'
],
'group3' => [
'c' => 'charlie',
'e' => 'easy'
]
]
I would like to remove any duplicate items completely; from the example above, I would like the following result:
[
'group1' => [
'b' => 'baker'
],
'group2' => [
],
'group3' => [
'e' => 'easy'
]
]
My current code is as follows:
foreach ($groups as $group_id => &$group_things) {
foreach ($group_things as $thing_id => $thing) {
foreach ($groups as $search_group_id => &$search_things) {
if ($search_group_id == $group_id) {
continue;
}
if (array_key_exists($thing_id, $search_things)) {
unset(
$group_things[$thing_id],
$search_things[$thing_id]
);
$ungrouped_things[$thing_id] = $thing;
}
}
}
}
This works, but has been roundly admonished by my colleagues. Is there a more elegant / less loopy way forward?