rocky
rocky

Reputation: 464

PHP - Detecting duplicate values in a nested array

I'm using an API which returns some JSON that I output in PHP.

PHP

$result = $api->sendRequest("getUsers", $inputParameters);
$output = json_decode($result, true);

An example of an array returned by the API. I can print out specific field values fine, but I can't figure out how to write a simple if statement that indicates whether or not there are duplicate names within the query result, specifically duplicate [fullName] fields as seen below.

Array
(
    [status] => Array
        (
            [request] => getUsers
            [recordsTotal] => 3
            [recordsInResponse] => 3
        )

    [records] => Array
        (
            [0] => Array
                (
                    [fullName] => Smith, Tom
                    [firstName] => Tom
                    [lastName] => Smith

                )

            [1] => Array
                (
                    [fullName] => Jones, Bill
                    [firstName] => Bill
                    [lastName] => Jones
                )

            [2] => Array
                (
                    [fullName] => Smith, Tom
                    [firstName] => Tom
                    [lastName] => Smith
                )

        )

)

Any help would be greatly appreciated.

Upvotes: 0

Views: 298

Answers (4)

War10ck
War10ck

Reputation: 12508

PHP has a lot of built in array functions to help with operations like this. You could try the following:

$names = array_column($output['records'], "fullName");
if(count(array_unique($names)) < count($names)) {
    ... /* handle duplicate values here */
}

In addition, $names contains a unique array of all the fullName columns from the original array for easy access and traversing. You can use this inside the above if statement to determine which names are duplicates like so:

$names_count = array_count_values($names);
foreach($names_count as $key => $value) {
    if(value > 1) {
        $dupes[] = $key;
    }
}

References:

PHP Array Functions

array_column()

array_unique()

array_count_values()

Upvotes: 0

didierc
didierc

Reputation: 14730

Simply create an array whose keys will be the fullnames of the entris you've seen so far:

$names = array();
foreach ($output['records'] as $entry){
    If (isset($names[$entry['fullname']]){
        // do some error processing
        echo "'${entry['fullname']}' is a duplicate";
    }
    $names[$entry['fullname']] = $entry;
}

You should have all the unique entries in $names.

Upvotes: 0

OdinX
OdinX

Reputation: 4211

This should work:

$data['records'] = array_map("unserialize", array_unique(array_map("serialize", $data['records'])));

Taken from here and slightly modified.

Upvotes: 1

cwurtz
cwurtz

Reputation: 3257

Not tested, but maybe try something like

function dupeCheck($array, $attribute = 'fullName') {
    $list = array();
    foreach($array['records'] as $value) {
        if(in_array($value[$attribute], $list))
            return true;
        $list[] = $value[$attribute];
    }
    return false;
}

Just iterating over the records, we maintain a list of values of whatever attribute, once it finds one that was already in the array, returns true.

Then just:

if(!dupeCheck($output, 'fullName')) { // no dupes in the API response }

Upvotes: 1

Related Questions