Пройдите по массиву, запомните, какие адреса используются какой командой. Когда по ключу (адресу) хранится более одной команды, вы нашли дубликат:
<?php
$teams = [
["id" => 1, "address" => "A1"]
,["id" => 2, "address" => "A1"]
,["id" => 3, "address" => "A2"]
];
function findDuplicates($teams) {
$addresses = [];
foreach ($teams as $team) {
if (!isset($addresses[$team["address"]])) {
$addresses[$team["address"]] = [];
}
$addresses[$team["address"]][] = $team["id"];
}
foreach ($addresses as $address => $teamsHere) {
if (count($teamsHere) > 1) {
echo "Teams with same address (" . $address . "): " . join(",", $teamsHere) . "\n";
}
}
}
findDuplicates($teams);
Попробуйте онлайн!
Редактировать: менее "неуклюжий" подход с использованием array_*
функций:
<?php
$teams = [
["id" => 1, "address" => "A1"]
,["id" => 2, "address" => "A1"]
,["id" => 3, "address" => "A2"]
];
function findDuplicates($teams) {
$addresses = array_column($teams, "address");
$counts = array_count_values($addresses);
return array_filter($teams, function($team) use ($counts) { return $counts[$team["address"]] > 1; });
}
print_r(findDuplicates($teams));
Попробуйте онлайн!