PHP Classes

XML to array in one simple call

Recommend this page to a friend!

      Generic XML parser class  >  Generic XML parser class package blog  >  Processing XML with a...  >  All threads  >  XML to array in one simple call  >  (Un) Subscribe thread alerts  
Subject:XML to array in one simple call
Summary:Use JSON calls to transform it
Messages:4
Author:bastien koert
Date:2012-09-13 14:23:21
Update:2012-12-05 19:18:45
 

  1. XML to array in one simple call   Reply   Report abuse  
Picture of bastien koert bastien koert - 2012-09-13 15:05:05
I found this when I needed to turn some XML into an array for processing

json_decode(json_encode((array) simplexml_load_string($xml)),1);

works great!

  2. Re: XML to array in one simple call   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-09-13 15:29:21 - In reply to message 1 from bastien koert
I wonder if it really needed to encode and decode it as JSON.

Anyway, extracting is just one part of the job that is usually necessary. Validating is often necessary to write robust software by assuring you are not processing unexpected data.

  3. Re: XML to array in one simple call   Reply   Report abuse  
Picture of Sean Benoit Sean Benoit - 2012-12-05 13:29:14 - In reply to message 1 from bastien koert
Love this suggestion, just used it on a project I'm working on.

  4. Re: XML to array in one simple call   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-12-05 19:18:45 - In reply to message 3 from Sean Benoit
I am glad you like it. It is so much simpler than having to write loads of code.