Convert php stdobject to an array

From Code Trash
Jump to: navigation, search

Got this code from a website... it is just type casting and it is working...

Just typecast

Why not just use typecasting? $data = (array) $data;

		$str = stripslashes($this->input->post('str'));
		$str = json_decode($str);
 
		$str = (array)$str;
		$post = array_merge($str,$hdetails);
		$post = array_merge($post,array('rdetails'=>$rdetails));

References http://www.sitepoint.com/forums/showthread.php?438748-convert-object-to-array.


PHP Object to Array

here is another code

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;
}