1

I'm trying to handle this data that I'm getting dynamically from an API. I know that the id's will always be unique, but the name's often match each other, which is a problem. Using PHP, how would I remove any stdClass Object whose name is identical to a previous object's name value from this array? I do specifically want the checks to progress from 0 to the highest value because there is other data in these stdClass Objects. In this case, I would want 1 to be removed because its name matches 0's name, but afterwards what is currently 2 should become the new 1 for obvious reasons.

Array
(
    [0] => stdClass Object
        (
            [id] => 6969
            [name] => Steve Jobs
        )
    [1] => stdClass Object
        (
            [id] => 2013
            [name] => Steve Jobs
        )
    [2] => stdClass Object
        (
            [id] => 1234
            [name] => The Woz
        )
)

Thanks!

NDM
  • 6,731
  • 3
  • 39
  • 52
tylerl
  • 1,160
  • 1
  • 19
  • 41
  • possible duplicate of [Select only unique array values from this array](http://stackoverflow.com/questions/11340450/select-only-unique-array-values-from-this-array) – ops Aug 06 '13 at 07:32

3 Answers3

11

You could use array_filter to do this:

$objects = array(
    (object)array('id' => 1, 'name' => 'test1'),
    (object)array('id' => 2, 'name' => 'test2'),
    (object)array('id' => 3, 'name' => 'test1'),
);
$known = array();
$filtered = array_filter($objects, function ($val) use (&$known) {
    $unique = !in_array($val->name, $known);
    $known[] = $val->name;
    return $unique;
});

check out this fiddle

NDM
  • 6,731
  • 3
  • 39
  • 52
  • Thanks, this looks like it might be better than @mkdotam's answer? I'll give it a try and mark this as correct when I'm sure it works :) – tylerl Aug 06 '13 at 07:43
  • I wouldn't say it is better by definition, every approach has its pros and cons. – NDM Aug 06 '13 at 07:49
  • Two things: First, why is there an array inside each stdClass Object? I'm still a newbie but AFAIK my example output is just normal stdClass Objects inside of an array... Second, which array am I supposed to be outputting? `$filtered`? Because that didn't work, and neither did `$known` or `$objects`... And there were unterminated left parenthesis on lines 2-4... – tylerl Aug 07 '13 at 02:43
  • you are right, I though it initialized the stdClasses like that, but it does not, I Will update the post to correctly add stdClasses – NDM Aug 07 '13 at 08:32
2

You can do something like this:

$newArray = array();

foreach ($array as $value) { $newArray[$value->name] = $value; }

By the end of that cycle you will have an array, where every name meets only once (and you will keep the latest occurrence of that name).

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
-1

I found the solution myself at http://seanmonstar.com/post/707138900/making-objects-in-an-array-unique

Put this function up at the top:

function unique_obj($obj) {
    static $idList = array();
    if(in_array($obj->id,$idList)) {
        return false;
    }
    $idList []= $obj->id;
    return true;
}

And just before you print/echo the array:

$amazingPeople = array_filter($amazingPeople,'unique_obj');
tylerl
  • 1,160
  • 1
  • 19
  • 41