phparraysobjectarray-unique

PHP Easier way to get unique in array of objects


I have been racking my head trying to think if there is a simpler method to grabbing the unique objects out of an array, but can't find any documentation or confirmation that anything exists that will do what I am asking.

I have an API returning the following:

Array
(
    [0] => stdClass Object
        (
            [Name] => Kory Kelly
            [PhoneNumber] => (555) 555-5555
            [EmailAddress] => kkelly@email.com
        )

    [1] => stdClass Object
        (
            [Name] => Kory Kelly
            [PhoneNumber] => (555) 555-5555
            [EmailAddress] => kkelly@email.com
        )

)

Now obviously I cannot use array_unique -- Since it checks actual values against array keys -- I have built a recursive function to tear apart the objects, rebuild as an array and do the checks manually during rebuild IE

if ( $arr['Name'] === $passed_arr['Name']
    && $arr['PhoneNumber'] === $passed_arr['PhoneNumber']
    && $arr['EmailAddress'] === $passed_arr['EmailAddress'] )

And that works .. But it seems a little long winded. Is there a built-in / more efficient approach to this? I feel like I am re-inventing the wheel with my current aproach.


Solution

  • If the JSON string of the objects is used as a key, this can also be implemented with a simple foreach loop.

    $arr = [
      (object)['Name' => 'Kory Kelly', 'PhoneNumber' => '(555) 555-5555'],
      (object)['Name' => 'Kory Kelly', 'PhoneNumber' => '(555) 555-5555'],
      (object)['Name' => 'Kory Kelly', 'PhoneNumber' => '(555) 555-5555X'],  //different
    ];
    
    foreach($arr as $key => $object){
      $arr[json_encode($object)] = $object;
      unset($arr[$key]);
    }
    $arr = array_values($arr);
    var_dump($arr);
    

    Try it yourself at 3v4l.org.

    Compared to array_unique($arr, SORT_REGULAR ), this approach only becomes interesting if only certain keys are to be taken into account in the object comparison.