Skip to content

Instantly share code, notes, and snippets.

@cebe
Created June 19, 2019 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cebe/aa02ee0a0d7f8ab0e3d0c10a7264692a to your computer and use it in GitHub Desktop.
Save cebe/aa02ee0a0d7f8ab0e3d0c10a7264692a to your computer and use it in GitHub Desktop.
workaround for object to array cast in PHP <7.2.0
<?php
// workaround for object to array cast in PHP <7.2.0
// https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
// https://3v4l.org/05SPE
private function objectToArray($object)
{
if (PHP_VERSION_ID < 70200) {
// work around PHP bug https://3v4l.org/05SPE
// https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
if (is_object($object)) {
$array = [];
foreach(get_object_vars($object) as $k => $v) {
$array[$k] = $v;
}
return $array;
}
}
return (array) $object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment