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!

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 :)
I wouldn't say it is better by definition, every approach has its pros and cons.
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...
you are right, I though it initialized the stdClasses like that, but it does not, I Will update the post to correctly add stdClasses
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).

Comments

-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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.