Convert multidimensional objects to array

You can use recursive function like below:

function object_to_array($obj, &$arr)
{
 if (!is_object($obj) && !is_array($obj))
 {
  $arr = $obj;
  return $arr;
 }

 foreach ($obj as $key => $value)
 {
  if (!empty($value))
  {
   $arr[$key] = array();
   objToArray($value, $arr[$key]);
  }
  else {$arr[$key] = $value;}
 }

 return $arr;
}

I know this is old but you could try the following piece of code:

$array = json_decode(json_encode($object), true);

where $object is the response of the API.


function convertObjectToArray($data) {
    if (is_object($data)) {
        $data = get_object_vars($data);
    }

    if (is_array($data)) {
        return array_map(__FUNCTION__, $data);
    }

    return $data;
}

Credit to Kevin Op den Kamp.