Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  1nflktd 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 Answers 4

up vote 1 down vote accepted

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 }
share|improve this answer
    
Thanks CJ, that did the trick. –  rocky Jun 18 '14 at 18:29

This should work:

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

Taken from here and slightly modified.

share|improve this answer
    
Thanks Chief, CJ's answer above seemed to work best. –  rocky Jun 18 '14 at 18:30

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.

share|improve this answer
    
Thanks didi, CJ's answer above seemed to work best. –  rocky Jun 18 '14 at 18:30
    
No problem, glad you got an answer. –  didierc Jun 18 '14 at 18:54

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

share|improve this answer
    
@Downvoter Care to comment?... –  War10ck Jun 19 '14 at 14:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.