Write a foreach loop for array of SimpleXMLElements

Posted by mjames on Stack Overflow See other posts from Stack Overflow or by mjames
Published on 2010-04-17T20:35:31Z Indexed on 2010/04/17 20:53 UTC
Read the original article Hit count: 168

Hi, I am using the XPath in PHP 5 to parse a XML document. The problem I have is writing a foreach to correctly display the following array:

XML document sample

value 1 value 2

           $xmlfile = 'link_to_file.xml';                       
            $xmlRaw = file_get_contents($xmlfile);      
            $xml = new SimpleXMLElement($xmlRaw);
            $install_files = $xml->xpath('//files');
            foreach($install_files as $row)
            {
              echo $row['file'];
            }

//var_dump for $row gives the following

               array(1) {
                  [0]=>
                  object(SimpleXMLElement)#21 (2) {
                    ["file"]=>
                    string(12) "value 1"
                    ["folder"]=>
                    string(8) "value 2"
                  }
                }

Ideally I would like to get the value by using $row['file'] or $row['folder']. Thanks for any help.

© Stack Overflow or respective owner

Related posts about php

Related posts about simplexml