/    Sign up×
Community /Pin to ProfileBookmark

object comparison

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=php]
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] =>
)

)

)[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@phpscripterauthorSep 25.2007 — Here's the solution: I could also do something similar to serialize but utilise hash instead. As I add the objects to an array, I have the hash being the array key, then before I add an object to the array I see if the hash already exists with array_key_exists(), if it does then I simply don't add it.

Actually I don't even need to check if the hash is already set, I can just add them all and duplicate keys will overwrite, so in the end my array will have only distinct objects.
×

Success!

Help @phpscripter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...