Page 1 of 1
Compare 2 associative array from xml, php 5
Posted: Sat Mar 08, 2008 12:39 am
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!
Re: Compare 2 associative array from xml, php 5
Posted: Sat Mar 08, 2008 9:43 am
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.
Re: Compare 2 associative array from xml, php 5
Posted: Sat Mar 08, 2008 11:23 am
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
Re: Compare 2 associative array from xml, php 5
Posted: Sat Mar 08, 2008 2:17 pm
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.
Re: Compare 2 associative array from xml, php 5
Posted: Sat Mar 08, 2008 2:46 pm
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!