object comparison

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yanisdon
Forum Newbie
Posts: 18
Joined: Thu Jul 06, 2006 10:44 pm

object comparison

Post by yanisdon »

Hi there,
I've got the following issue:

I've menaged to parse RSS data from a given XML file into a PHP object with SimpleXML. As this is quiet a trivial task, so far so good.
At a particluar spot within the code I have the following object instances.

Here's a print_r($obj);

Two first two object instances are equal because they have the same attributes and values, wheras the third one is different.
What I like to be doing here before going any further is to get rid of duplicates and only take unequal instances to proced with my methods and code.

My idea now is to compare a particular attribute (E.g. $obj->title) and if that attribute appears to exist more than once, get rid of the/all duplicate/s.

Something like DISTINCT in SQL if that make sense :)

How can I get that going?

Cheers!

Code: Select all

stdClass Object
(
    [type] => cbd
    [uid] => 1
    [status] => 1
    [promote] => 1
    [sticky] => 1
    [title] => Test 1
    [body] => This CATS (Creating Accessible Teaching & Support) 
    [teaser] => This CATS (Creating Accessible Teaching & Support) 
    [ftype] => RSS
    [fversion] => 2.0
    [fencoding] => UTF-8
    [flanguage] => en
    [fmanagingeditor] => 2
    [cmetadata_size] => 0
    [taxonomy] => Array
        (
            [tags] => Array
                (
                    [19] => 
                    [46] => 
                    [45] =>  Curriculum, Inclusive design, Curriculum, Learning design approaches
                    [5] => 
                )

        )

)

stdClass Object
(
    [type] => cbd
    [uid] => 1
    [status] => 1
    [promote] => 1
    [sticky] => 1
    [title] => Test 1
    [body] => This CATS (Creating Accessible Teaching & Support) 
    [teaser] => This CATS (Creating Accessible Teaching & Support) 
    [ftype] => RSS
    [fversion] => 2.0
    [fencoding] => UTF-8
    [flanguage] => en
    [fmanagingeditor] => 2
    [cmetadata_size] => 0
    [taxonomy] => Array
        (
            [tags] => Array
                (
                    [19] => 
                    [46] => 
                    [45] =>  Curriculum, Inclusive design, Curriculum, Learning design approaches
                    [5] => 
                )

        )

)

stdClass Object
(
    [type] => cbd
    [uid] => 1
    [status] => 1
    [promote] => 1
    [sticky] => 1
    [title] => Test 2
    [body] => The use and efficacy of a simulation model 
    [teaser] => The use and efficacy of a simulation model 
    [ftype] => RSS
    [fversion] => 2.0
    [fencoding] => UTF-8
    [flanguage] => en
    [fmanagingeditor] => 2
    [cmetadata_size] => 0
    [taxonomy] => Array
        (
            [tags] => Array
                (
                    [19] =>  Engineering and Related Technologies, Civil Engineering
                    [46] => 
                    [45] =>  Flexible learning, Learning designs, Simulation-based learning
                    [5] => 
                )

        )

)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You could serialize() each object and see if the serializations match. Perhaps store them in an array and use array_unique() on it.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

You could also do something similar to serialize but use hash instead. As you add the objects to an array, have the hash be the array key, then before you add an object to the array see if the hash already exists with array_key_exists(), if it does then don't add it.

Actually you don't even need to check if the hash is already set, you can just add them all and duplicate keys will overwrite, so in the end your array will have only distinct objects.
yanisdon
Forum Newbie
Posts: 18
Joined: Thu Jul 06, 2006 10:44 pm

Post by yanisdon »

Thanks a lot. That has helped.
Post Reply