• XML to Array

    by  • March 20, 2005 • PHP, XML • 22 Comments

    I had a problem were i couldn’t get my xml feeds into an array so i got these functions that do it for you and make your life so much easier to handle, makes putting xml feeds into a db so much easier.

    $url – is the location of the xml feed.

    and $data is the xml into the array.

    enjoy

    Removed the large source, it is available through the link below, wasn’t validating as xhtml :)
    Plain text source : http://blog.whoooop.co.uk/xml.txt

    use this snippet post a comment of any improvements you make and were you used it.

    Related Posts

    • No Related Post

    22 Responses to XML to Array

    1. Fatih
      March 23, 2005 at 5:29 pm

      Thanks for such a useful code… That is waht I needed…

    2. Matt
      March 28, 2005 at 10:49 pm

      Fantastic. Fast and exactly what I was looking for. Thanks!

    3. March 31, 2005 at 3:15 am

      I think this is great, however it seems to be missing one attibute function. When you load an xml into it where the father node has attributes AND childNodes it gets the value of the childNodes into array, however if it’s just nodes with attributes it puts the attributes into the array. It would be great if it somehow did both. An example is this flickr xml

    4. cristi
      March 31, 2005 at 6:07 pm

      The script don’t work in PHP5. :(

    5. CaShY
      March 31, 2005 at 6:16 pm

      theres an easy fix for it just can’t for the life of me think what it was.

      Never mind hopefully someone will post how to

    6. CaShY
      March 31, 2005 at 6:20 pm

      basically

      SNIP]
      function GetXMLTree ($xmldata)
      {
      $attributes =array();

      // we want to know if an error occurs
      ini_set (‘track_errors’, ’1′);
      [SNIP]

      makes it work in php5

    7. cristi
      April 2, 2005 at 8:47 am

      Thanks, CaShY. You help me very much.

    8. ibrahim
      April 19, 2005 at 10:40 am

      any idea of how to fix the problem that Michael pointed out ?

      Thanx

    9. ibrahim
      April 19, 2005 at 10:41 am

      with this simple xml file , i get all attributes but not the name of the city

      exemple :

      BC
      516521
      -1212891
      -800
      1

    10. Jose
      April 22, 2005 at 10:02 am

      Very useful code but I had problems with empty xml files, for example:

      <NAME> </NAME>

      I fix the problem changing the call to the funtion GetChildren in the main function….

      —– original ——
      $result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, ‘open’));
      —– fixed ——–
      $result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, $vals[$i]['type']));

      Sorry for my poor english and thanks for the code.

    11. simon
      May 20, 2005 at 7:59 pm

      wow,wonderful~
      thx u very much,that’s what I needed

    12. May 23, 2005 at 11:44 pm

      Wow, worked like a charm ! Thanks a lot, mate – i just spent an hour trying to get an XML feed to work and needed 5 minutes with your script :)

    13. cid73
      May 26, 2005 at 11:09 am

      very useful and very nice code, thanks, CaShy!

      a little problem:

      somehow I need to keep original aspect of the data in my xml file to output, however, when I change

      xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1)

      into

      xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 0)

      and it can’t work.

      pls fix this problem, I’ll be still waiting. Thanx again(and sorry for my poor English).

    14. kris
      January 18, 2006 at 4:16 pm

      Very useful script – thanks.

      I have one small problem with the script though.

      If there is only 1 child element the ARRAY looks like this:
      —————————
      Array
      (
      [sync] => Array
      (
      [learner_id] => 1234
      [learner_name] => Father Christmas
      [password] => hoho
      [client_id] => Companyname
      [device_id] => 023408fsdf8032
      [last_sync] => 20051214132201
      [event] => Array
      (
      [core] => course details
      [engine_name] => ultaextreme
      [engine_version] => 1.0.0
      [extended] => answers
      )
      )
      )
      —————————

      I REALLY NEED IT TO LOOK LIKE THIS:
      —————————
      Array
      (
      [sync] => Array
      (
      [learner_id] => 1234
      [learner_name] => Father Christmas
      [password] => hoho
      [client_id] => Companyname
      [device_id] => 023408fsdf8032
      [last_sync] => 20051214132201
      [event] => Array
      (
      [0] => Array
      (
      [core] => course details
      [engine_name] => ultaextreme
      [engine_version] => 1.0.0
      [extended] => answers
      )

      )

      )

      )
      —————————
      How can this be done?

      Thanks again for a great script…

    15. John
      February 9, 2006 at 10:39 pm

      Hi,

      Great script, It is doing exactly what I need it to do. I have a couple questions, and I have to first say I know just enough php to get me in trouble.

      Im using your script in my wordpress blog and have 3 areas that the results will appear in left side bar (lside.php) the content or post section (index.php) and the right side (rside.php)

      What I want to do is have results 1,2 & 3 in my left side bar, 4-9 in the center (index) and 1- 15 in the right side bar (rside.php)

      To complicate the issue a variable will be place in the xml feed url that is being fetched.

      I have been able to pass the var to the url xml string and everything worked – the data was passed back flawlessly.

      Here is where my stupidity comes into play.

      How do i take a var and echo the data on the page.

      The file being returned is:

      Heading, url, uri, description but keep in mind this needs to be placed in 3 different areas of the page and three files are included to actually build the page.

      Any help you can give me will be greatly appreciated as I have been searching for a script that would do this for some time now.

      Thanks John

    16. March 2, 2006 at 3:34 am

      Nice script

      If you want to use this with PHP5 change line 25:

      $result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, ‘open’));

      To

      $result [$vals [$i]['tag']] = array_merge ((array)$attributes, (array)GetChildren ($vals, $i, ‘open’));

    17. Pingback: Taste of Tech » Blog Archive » SaveLinks

    18. Pingback: Taste of Tech | SaveLinks

    19. Rockstar
      July 10, 2009 at 10:06 am

      Thanks for the simple n elegent script !!!

    20. dave
      August 7, 2009 at 1:18 pm

      So where is the link to get the script??

    21. phpguy
      September 29, 2009 at 5:28 pm

      it so helpful,thanks a ton

    22. mick
      December 23, 2009 at 10:11 pm

      What a great script! Thanks!

    Leave a Reply

    Your email address will not be published. Required fields are marked *