How to convert simplexmlobject into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bandy
    New Member
    • Mar 2010
    • 25

    How to convert simplexmlobject into an array

    I have XML that I am not able to get it into an array.
    Code:
     
    <users>
               <user>
                        <id>1  </id>
                        <nm> xyz </nm>
                        <joined>
                                    <date>  </date>
                                    <venue>  </date>
                        </joined>
                </user>
     </users>
    This xml is in:
    Code:
        $data1 = simplexml_load_string($xml);
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Code:
    <?php
    $x = new stdClass;
    $x->y = new stdClass;
    $x->y->a = 1;
    $x->y->b = 2;
    $x->r = 1;
    $x->e = 3;
    
    var_dump((array)$x);

    Comment

    • bandy
      New Member
      • Mar 2010
      • 25

      #3
      Originally posted by Markus
      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.

      Code:
      <?php
      $x = new stdClass;
      $x->y = new stdClass;
      $x->y->a = 1;
      $x->y->b = 2;
      $x->r = 1;
      $x->e = 3;
      
      var_dump((array)$x);
      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.

      Comment

      Working...