By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

How to convert simplexmlobject into an array

P: 25
I have XML that I am not able to get it into an array.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <users>
  3.            <user>
  4.                     <id>1  </id>
  5.                     <nm> xyz </nm>
  6.                     <joined>
  7.                                 <date>  </date>
  8.                                 <venue>  </date>
  9.                     </joined>
  10.             </user>
  11.  </users>
  12.  
  13.  
This xml is in:
Expand|Select|Wrap|Line Numbers
  1.     $data1 = simplexml_load_string($xml);
  2.  
May 22 '10 #1
Share this Question
Share on Google+
2 Replies


Markus
Expert 5K+
P: 6,050
Why do you need it as an array? You can cast it to an array. However, you'll have to do that for each nested object.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $x = new stdClass;
  4. $x->y = new stdClass;
  5. $x->y->a = 1;
  6. $x->y->b = 2;
  7. $x->r = 1;
  8. $x->e = 3;
  9.  
  10. var_dump((array)$x);
  11.  
May 22 '10 #2

P: 25
@Markus
I didn't get above code. Can you please elaborate on this because working with this first time? Actually I am developing an app in which I need to convert this simple xml object into simple array. I was able to convert into an array when there was one simplexmlobject, but this array has more than one such objects.

I will really appreciate your help.
May 24 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.