Created
July 17, 2020 01:19
-
-
Save dextervip/995fdcd59caf1dcf004565447de36ab4 to your computer and use it in GitHub Desktop.
Auto decode DateTime Objects in json_decode PHP
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 | |
$data = json_decode($message->getBody(), true); | |
function array_map_recursive($callback, $array) | |
{ | |
$func = function ($item) use (&$func, &$callback) { | |
if(is_array($item) && isset($item['date']) && isset($item['timezone_type'])){ | |
return call_user_func($callback, $item); | |
} | |
return is_array($item) ? array_map($func, $item) : call_user_func($callback, $item); | |
}; | |
return array_map($func, $array); | |
} | |
$data = array_map_recursive(function ($item){ | |
if(is_array($item) && isset($item['date']) && isset($item['timezone_type'])){ | |
return new \DateTime($item['date']); | |
} | |
return $item; | |
},$data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment