简体   繁体   中英

Check same value in array

I have the following array:

$teams = [["id" => 1, "address" => "A1"],["id" => 2, "address" => "A1"],["id" => 3, "address" => "A2"]];

How can I check and get the teams with the same address? My output should be Team ID 1 and 2? Consider that I cannot use hard coded address. The data is dynamic and is coming from the database.

In php, laravel.

Thank you in advance!

First you need to group by their address and then you can use array_filter() to truncate your array based on criteria:

<?php
$teams = [["id" => 1, "address" => "A1"],["id" => 2, "address" => "A1"],["id" => 3, "address" => "A2"]];

// Set a new array
$filtered = [];

// Loop the teams
foreach($teams as $v)
{
    // Group the teams into their respective addresses
    $filtered[$v['address']][] = $v;
}

// Filter out any address with 1 or fewer teams
$filtered = array_filter($filtered, function($v){
    return count($v) > 1;
});

print_r($filtered);

// Now you can loop $filtered and display whatever you want

Output:

Array
(
    [A1] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [address] => A1
                )

            [1] => Array
                (
                    [id] => 2
                    [address] => A1
                )

        )

)

Another way to do this is to use array_column() and array_count_values() . Then use array_filter() to remove elements with no duplicates:

$teams = [
 ["id" => 1, "address" => "A1"],
 ["id" => 2, "address" => "A1"] ,
 ["id" => 3, "address" => "A2"]
];

$dups = array_count_values(array_column($teams, 'address'));
$teams = array_filter($teams, function($item) use($dups) {
    return $dups[$item['address']] > 1;
});
print_r($teams);

Outputs (reformatted):

Array
(
    [0] => Array([id] => 1, [address] => A1)
    [1] => Array([id] => 2, [address] => A1)
)

Go through the array, remember which addresses are used by which team. When there is more than one team stored at a key (address), you found a duplicate:

<?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);

Try it online!


Edit: a less "clunky" approach, using array_* functions:

<?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));

Try it online!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM