0

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.

rocky
  • 464
  • 6
  • 22
  • What are you doing with this array ? Inserting in a table ? If so, I'd recommend that you would check if that value is already inserted. – hlscalon Jun 18 '14 at 18:04
  • Do these users have unique id's associated with them. Just curious, but _Tom Smith_ seems like it could be a fairly common name. Can you be absolutely sure these are not different users?... – War10ck Jun 18 '14 at 18:15
  • @1nflktd Nope, just printing out specific values to HTML. So if there are duplicate [fullName] results, I want to print out a simple line of text indicating that duplicates were detected. – rocky Jun 18 '14 at 18:16
  • @War10ck They do have unique User IDs associated with them but I left them out for the sake of simplicity. Anyway, the IDs aren't really relevant, I just need to know if there are duplicate names. – rocky Jun 18 '14 at 18:18

4 Answers4

1

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 }
cwurtz
  • 3,177
  • 1
  • 15
  • 15
1

This should work:

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

Taken from here and slightly modified.

Community
  • 1
  • 1
OdinX
  • 4,135
  • 1
  • 24
  • 33
0

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.

didierc
  • 14,572
  • 3
  • 32
  • 52
0

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()

War10ck
  • 12,387
  • 7
  • 41
  • 54