Created
June 19, 2019 08:36
-
-
Save cebe/aa02ee0a0d7f8ab0e3d0c10a7264692a to your computer and use it in GitHub Desktop.
workaround for object to array cast in PHP <7.2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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