Search code examples
phparray-difference

array_diff for a complex xml


I have this xml that contains four text messages and i have converted that into an array.

The xml

> <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
> <?xml-stylesheet type="text/xsl" href="sms.xsl"?> <smses count="4">  
> <sms protocol="0" address="0800000" date="1328814938421" type="2"
> subject="null" body="Its raining cat and dogs;and their owners."
> toa="null" sc_toa="null" service_center="null" read="1" status="-1"
> locked="0" date_sent="null" readable_date="Feb 9, 2012 10:15:38 PM"
> contact_name="Hans Petit" />
>      <sms protocol="0" address="08005678" date="1328814938421" type="2" subject="null" body="Hello,Andy.The attachment wasn't
> sent.Please resend." toa="null" sc_toa="null" service_center="null"
> read="1" status="-1" locked="0" date_sent="null" readable_date="Feb 9,
> 2012 10:15:38 PM" contact_name="Mary The Great" />
>      <sms protocol="0" address="080091011" date="1328814938421" type="2" subject="null" body="Lorem Ipsum = Good Night." toa="null"
> sc_toa="null" service_center="null" read="1" status="-1" locked="0"
> date_sent="null" readable_date="Feb 9, 2012 10:15:38 PM"
> contact_name="Ed Myers" />
>      <sms protocol="0" address="+44839202" date="1328815215841" type="1" subject="null" body="I represent a variable." toa="null"
> sc_toa="null" service_center="+4422500000" read="1" status="-1"
> locked="0" date_sent="null" readable_date="Feb 9, 2012 10:20:15 PM"
> contact_name="Dexter" />
>      <sms protocol="0" address="+2273839309" date="1329194575094" type="1" subject="null" body="Take it easi" toa="null" sc_toa="null"
> service_center="+4422500000" read="1" status="-1" locked="0"
> date_sent="null" readable_date="Feb 14, 2012 7:42:55 AM"
> contact_name="Miguel" />   </smses>

The array

Array ( [smses] => Array ( [sms] => Array ( [0] => Array ( ) [1] => Array ( ) [0_attr] => Array ( [protocol] => 0 [address] => 0800000 [date] => 1328814938421 [type] => 2 [subject] => null [body] => Its raining cat and dogs;and their owners. [toa] => null [sc_toa] => null [service_center] => null [read] => 1 [status] => -1 [locked] => 0 [date_sent] => null [readable_date] => Feb 9, 2012 10:15:38 PM [contact_name] => Hans Petit ) [1_attr] => Array ( [protocol] => 0 [address] => 08005678 [date] => 1328814938421 [type] => 2 [subject] => null [body] => Hello,Andy.The attachment wasn't sent.Please resend. [toa] => null [sc_toa] => null [service_center] => null [read] => 1 [status] => -1 [locked] => 0 [date_sent] => null [readable_date] => Feb 9, 2012 10:15:38 PM [contact_name] => Mary The Great ) [2] => Array ( ) [2_attr] => Array ( [protocol] => 0 [address] => 080091011 [date] => 1328814938421 [type] => 2 [subject] => null [body] => Lorem Ipsum = Good Night. [toa] => null [sc_toa] => null [service_center] => null [read] => 1 [status] => -1 [locked] => 0 [date_sent] => null [readable_date] => Feb 9, 2012 10:15:38 PM [contact_name] => Ed Myers ) [3] => Array ( ) [3_attr] => Array ( [protocol] => 0 [address] => +44839202 [date] => 1328815215841 [type] => 1 [subject] => null [body] => I represent a variable. [toa] => null [sc_toa] => null [service_center] => +4422500000 [read] => 1 [status] => -1 [locked] => 0 [date_sent] => null [readable_date] => Feb 9, 2012 10:20:15 PM [contact_name] => Dexter ) [4] => Array ( ) [4_attr] => Array ( [protocol] => 0 [address] => +2273839309 [date] => 1329194575094 [type] => 1 [subject] => null [body] => Take it easi [toa] => null [sc_toa] => null [service_center] => +4422500000 [read] => 1 [status] => -1 [locked] => 0 [date_sent] => null [readable_date] => Feb 14, 2012 7:42:55 AM [contact_name] => Miguel ) ) ) [smses_attr] => Array ( [count] => 4 ) )

I have another xml doc 2 which i have changed only one value - address="0800000" in the first which is address="0900000" in the second.When i compare the two arrays using

include "xml2array.php";
$contents = file_get_contents('sms.xml');//Or however you what it
$result = xml2array($contents);
//print_r($result);

$contents_ = file_get_contents('smsz.xml');//Or however you what it
$result_ = xml2array($contents_);
//print_r($result_);

$result_diff = array_diff($result, $result_);

print_r($result_diff);

i get which is not what i was expecting.

Array ( )

I was expecting something like

Array
(
    [1] => 0900000
)

Solution

  • You have a multi-dimsensional array and array_diff only support one dimension.

    From the manual about array_diff:

    This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.