About conversion of simplexmlobject to array.

Posted by Rishi2686 on Stack Overflow See other posts from Stack Overflow or by Rishi2686
Published on 2010-05-26T12:12:58Z Indexed on 2010/05/26 14:01 UTC
Read the original article Hit count: 276

Filed under:
|
|

Hi guys,

I tried the way you told but really messing up on some points. I am getting array fields but when it comes to children nodes, i go off the track. here giving single user's simplexml object only :

SimpleXMLElement Object ( [users] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => array ) [user] => Array ( [0] => SimpleXMLElement Object ( [id] => 1011 [name] => saroj [location] => SimpleXMLElement Object ( ) [description] => SimpleXMLElement Object ( ) [profile_image_url] => http://a5.example.com/profile_images/img_normal.jpg [url] => SimpleXMLElement Object ( ) [created_at] => Fri Apr 16 17:04:13 +0000 2010 [status] => SimpleXMLElement Object ( [created_at] => Wed May 26 02:56:03 +0000 2008 [id] => 1473 [text] => hi enjoying here! [in_reply_to_user_id] => SimpleXMLElement Object ( ) ) )

To get this into array I am writing code as below :

  $users = simplexml_load_string($xml);   
  $arr2 = array(); 
foreach($users->users->user as $user) 
{
   $arr1 = array(); 
  foreach($user as $k=>$v) 
  {
      $arr1[$k] = (string) $v; 
  } 
  $arr2[] = $arr1; 
}

I am getting all the fields as expected from arr2() instead of the status field which contains an array (imp: id & created_at fields repeated), it gives just key as status. While trying differently i get some of the fields of status array too. Please help me on this, i am really trying it from last two days. Help will be greatly appreciated.

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml