Compare 2 associative array from xml, php 5

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
clrockwell79
Forum Newbie
Posts: 4
Joined: Thu Mar 06, 2008 3:24 pm

Compare 2 associative array from xml, php 5

Post by clrockwell79 »

I have two arrays that are created from two separate xml files and I need to compare the two. Can anyone suggest a 'best way' of doing this?

Both arrays are created from an xml file with the same schema:
<root>
<Company>
<CompanyName></CompanyName>
<Phone></Phone>
</Company>
</root>

I need to know if $file1[0]['Company']['CompanyName'] is the same as $file2[0]['Company']['CompanyName']

Please let me know if I can provide any more info. Thanks in advance!
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Compare 2 associative array from xml, php 5

Post by yacahuma »

use
simplexml_load_file

Code: Select all

 
$obj1 = simplexml_load_file('test1.xml');
$obj2 = simplexml_load_file('test2.xml');
 
if ($obj1['CompanyName'] == $obj2['CompanyName'])
 
 
is going to be something similar to that. When you try it, just to a print_r on obj to see the structure.Then you now exactly how to compare it.
clrockwell79
Forum Newbie
Posts: 4
Joined: Thu Mar 06, 2008 3:24 pm

Re: Compare 2 associative array from xml, php 5

Post by clrockwell79 »

I should have been more specific, apparently late night posting does not work out too well. I need to find the differences between the two arrays

Using the simplexml_load_object I get about 1100 objects:
array ( 0 => SimpleXMLElement Object
(
[Company] => a
[Booth] => 12164
[Address] => XXXXXXXX
[Locale] => XXXXXXXXXXXXXXX
[Phone] => XXXXXXXX
[Contact] => Erik XXXXXXX
[WillDisplay] => Will Display:
[Product] => XXXXXXXXXXXXXX
)

)

The second will most often have the exact same output, but I need to know if any detail is different. Thanks for the suggestion and any future suggestions
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Compare 2 associative array from xml, php 5

Post by yacahuma »

same thing

I think you should be able to do a
foreach ($obj as $k=>$v)
if ($obj[$k] == $obj2[$k])

depending on the structure something like that should work. Again I am guesstimating here without all the details.
clrockwell79
Forum Newbie
Posts: 4
Joined: Thu Mar 06, 2008 3:24 pm

Re: Compare 2 associative array from xml, php 5

Post by clrockwell79 »

Thanks yacahuma

I was accessing the object incorrectly, using:

Code: Select all

$export->DirectoryExhibitorListings[$i]->Phone
works the way it should.

Also, something of note, when comparing these objects you must first convert them to a string:

Code: Select all

(string)$export->DirectoryExhibitorListings[$i]->Phone
or the comparison just doesn't work

Thanks much again!
Post Reply