object to array

PHP Add comments

I used xmlhttp request to send multiple data from javascript to php. I created objects and properties (virtually it is referred as Associative Array in javascript). And finally¬† all the objects are encapsulated into a main object and this was stringified by using JSON method… finally i got a json string which i sent using xmlhttp.

When i received the string in php and debugged it and is said all as StdObjects and when i tried to use in foreach it didnt work. So i searched for the following and i converted the StdObject to array using the following. Hope people might use it.

If anybody willing to comment on its performance or any other idea then please do so…

 
function object_to_array($data) 
{
  if(is_array($data) || is_object($data))
  {
    $result = array(); 
    foreach($data as $key => $value)
    { 
      $result[$key] = object_to_array($value); 
    }
    return $result;
  }
  return $data;
}

Leave a Reply

Entries RSS