4

What would be the most efficient way of either removing dups or creating new array with no dups? Are there any built in PHP functions that save this manual removal?

[2013-05-27 22:35:55]: Array
(
    [0] => stdClass Object
        (
            [type] => 1
            [val] => 1111
        )

    [1] => stdClass Object
        (
            [type] => 1
            [val] => 2222
        )

    [2] => stdClass Object
        (
            [type] => 1
            [val] => 1111
        )

    [3] => stdClass Object
        (
            [type] => 1
            [val] => 2222
        )
)

Many thanks, Luke

3

3 Answers 3

3

I doubt that there is anything faster than a foreach() loop in this case.

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

Comments

2

Well first of all, you have to decide on what you mean with duplicates, for example in the array above, the items in 0 and 2 could refer to the same instance, or it could be two completely different instances.

Take a look here to see more about comparing objects in PHP.

In short, if by duplicate you mean a reference to the same object, then you're in luck because that's really easy using array_unique:

array_unique($array);

Will do the job.

On the other hand if you want to remove objects that have the same values, which I'm guessing is less likely, then you can go with array_filter which takes a callback function for you to remove the items in the array. I leave the programming of the callback to you. ;)

Comments

0

array_unique should be able to help you. array_unique

See this codepad here

1 Comment

That just work for non Object values, different from the original question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.