Deividas Vysniauskas
Deividas Vysniauskas

Reputation: 71

Find rows in a 2d array where values in a specific column occur more than once

I've been stuck with this for a little while now and all I can find on the internet is how to remove the duplicates. I'm new to PHP so simple answers would be very well appreciated.

So let's say I have a multidimensional array of surnames, addresses, time of accidents and reason of accidents like so:

[
    ['Adams','King Street 12','14:25','Heart Attack'],
    ['Ellis','Vine Street 4','02:48','Broken Leg'],
    ['Adams','Parker Street 43','20:10','Heart Attack'],
]

I need to find entries that have had the same accident more than once, so of this short array, the output should look something like this:

Adams   King Street 12   14:25   Heart Attack
Adams   Parker Street 43   20:10   Heart Attack

I tried $answer = array_unique(array_diff_assoc($whole_ar, array_unique($whole_ar))); but this doesn't seem to work on multidimensional arrays.

Upvotes: 3

Views: 83

Answers (2)

mickmackusa
mickmackusa

Reputation: 47894

Make a single loop over the input. Populate a temporary lookup array and populate it with the first occurrence of each unique value in column 4 (key [3]). On the second occurrence of a given value, push the cached first row and the current row into the result array. I'll keep things separated by using the unique values as first level keys in the result array. Once the result array contains a given value, then any subsequent encounters can be pushed directly into the result array without checking the temporary array.

Code: (Demo)

$array = [
    ['Adams',   'King Street 12',   '14:25', 'Heart Attack'],
    ['Ellis',   'Vine Street 4',    '02:48', 'Broken Leg'],
    ['Adams',   'Parker Street 43', '20:10', 'Heart Attack'],
    ['Neil',    'Jack Street 9',    '12:25', 'Broken Heart'],
    ['Colin',   'Queen Street 18',  '05:48', 'Restless Leg'],
    ['Artemis', 'First Street 1',   '10:17', 'Broken Leg'],
    ['Rufus',   'Long Street 5',    '11:11', 'Dog Attack'],
];

$result = [];
foreach ($array as $row) {
    if (isset($result[$row[3]])) {
        $result[$row[3]][] = $row;
    } elseif (isset($first[$row[3]])) {
        $result[$row[3]] = [$first[$row[3]], $row];
    } else {
        $first[$row[3]] = $row;
    }
}
var_export($result);

Output (from my sample data):

array (
  'Heart Attack' => 
  array (
    0 => 
    array (
      0 => 'Adams',
      1 => 'King Street 12',
      2 => '14:25',
      3 => 'Heart Attack',
    ),
    1 => 
    array (
      0 => 'Adams',
      1 => 'Parker Street 43',
      2 => '20:10',
      3 => 'Heart Attack',
    ),
  ),
  'Broken Leg' => 
  array (
    0 => 
    array (
      0 => 'Ellis',
      1 => 'Vine Street 4',
      2 => '02:48',
      3 => 'Broken Leg',
    ),
    1 => 
    array (
      0 => 'Artemis',
      1 => 'First Street 1',
      2 => '10:17',
      3 => 'Broken Leg',
    ),
  ),
)

Upvotes: 0

mantis
mantis

Reputation: 56

I guess you will have to do this in steps firstly you will need php5.5 or higher then put a array_column() on each key something like so

$myArray = array(
    array(
    'Adams','King Street 12','14:25','Heart Attack'
    ),
    array(
    'Ellis','Vine Street 4','02:48','Broken Leg'
    ),
    array(
    'Adams','Parker Street 43','20:10','Heart Attack'
    )
);

Next you call and find duplicates of the first Key (Names)

$name = array_column($myArray, '0');
$address = array_column($myArray, '1');
$time = array_column($myArray, '2');
$injury = array_column($myArray, '3');

Next we count the occurences

$count_names = array_count_values($name);

Do this for each criteria

Then select the apropriate arrays using an if construct,

if ($count_names > 0){
...
}

Finaly select the apropriate data rows using the result of the if construct

Upvotes: 3

Related Questions