Skip to content

Instantly share code, notes, and snippets.

@peshi
Last active October 26, 2022 08:10
Show Gist options
  • Save peshi/8902621 to your computer and use it in GitHub Desktop.
Save peshi/8902621 to your computer and use it in GitHub Desktop.
Convert multidimensional stdClass object(s) to array.
<?php
/**
* Convert multidimensional stdClass object(s) to array
*
* @param $object
* @return array
*/
private function objectToArray($object) {
if (is_object($object)) {
$object = get_object_vars($object);
}
if (is_array($object)) {
return array_map(array($this, 'objectToArray'), $object);
} else {
return $object;
}
}
@peshi
Copy link
Author

peshi commented Feb 9, 2014

You would receive the same result if you encode your object(s) to json and decode it.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment