I have an xml document I am pulling in with simple xml load file. I simply want loop my array with foreach but I can't seem to do it like I would expect with a normal array.

I tried:

foreach($xmldata['program'] as $key=>$value) {
echo $value['@attributes']['country']
}

but it echos nothing. Any ideas of how I am suppose to do this with an xml document? Below you can see a portion of my xml document and the array I get when I do a print_r after simplexml importing it.

example xml document

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<programs>
<program country="Argentina" region="LA" institution="University of Buenos Aires" location="Buenos Aires" adbpartner="College" academicunit="Agriculture" collegedesignation="Agriculture" agreetment="MOA" dateestablished="ND" adbcontact="OIPA" expirationdate="ND" lettersofsupport="N" comments="no date on agreement"/>
<program country="Argentina" region="LA" institution="Universidad Catolica de Cordoba" location="Cordoba" adbpartner="adb" academicunit="University" agreetment="MOA" dateestablished="35248" adbcontact="Provost" expirationdate="ND" lettersofsupport="N" comments="no date on agreement" estsortdate="1996"/>
</programs>

array output after simplexml load file:

SimpleXMLElement Object
(
[program] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[country] => Argentina
[region] => LA
[institution] => University of Buenos Aires
[location] => Buenos Aires
[adbpartner] => College
[academicunit] => Agriculture
[collegedesignation] => Agriculture
[agreetment] => MOA
[dateestablished] => ND
[adbcontact] => OIPA
[expirationdate] => ND
[lettersofsupport] => N
[comments] => no date on agreement
)

            )

        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [country] => Argentina
                        [region] => LA
                        [institution] => Universidad Catolica de Cordoba
                        [location] => Cordoba
                        [adbpartner] => adb
                        [academicunit] => University
                        [agreetment] => MOA
                        [dateestablished] => 35248
                        [adbcontact] => Provost
                        [expirationdate] => ND
                        [lettersofsupport] => N
                        [comments] => no date on agreement
                        [estsortdate] => 1996
                    )

            )

    You sure it's not just $value['country']?

      Write a Reply...