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!
Compare 2 associative array from xml, php 5
Moderator: General Moderators
-
clrockwell79
- Forum Newbie
- Posts: 4
- Joined: Thu Mar 06, 2008 3:24 pm
Re: Compare 2 associative array from xml, php 5
use
simplexml_load_file
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.
simplexml_load_file
Code: Select all
$obj1 = simplexml_load_file('test1.xml');
$obj2 = simplexml_load_file('test2.xml');
if ($obj1['CompanyName'] == $obj2['CompanyName'])
-
clrockwell79
- Forum Newbie
- Posts: 4
- Joined: Thu Mar 06, 2008 3:24 pm
Re: Compare 2 associative array from xml, php 5
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
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
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.
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
Thanks yacahuma
I was accessing the object incorrectly, using:
works the way it should.
Also, something of note, when comparing these objects you must first convert them to a string: or the comparison just doesn't work
Thanks much again!
I was accessing the object incorrectly, using:
Code: Select all
$export->DirectoryExhibitorListings[$i]->PhoneAlso, something of note, when comparing these objects you must first convert them to a string:
Code: Select all
(string)$export->DirectoryExhibitorListings[$i]->PhoneThanks much again!