tags:

views:

32

answers:

1

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.

A: 
$users = simplexml_load_string($xml);   

function recursive_obj2array($obj, &$subject_array=array()) {
   foreach ((array) $obj as $key => $var) {
       if (is_object($var)) {
           if(count((array) $var) == 0) {
               $subject_array[$key] = 'NULL';
           }
           else {
               recursive_obj2array($var, $subject_array[$key]);
           }
       }
       else {
           $subject_array[$key] = $var;
       }
   }
}

$gimmie = array();

recursive_obj2array($users , $gimmie);

then:

foreach ( $gimmie as $key => $val )

you can also check this:

aSeptik
getting nothing out of the function.
Rishi2686
have you tried with `print_r($gimmie);` ?
aSeptik