Skip to content

Instantly share code, notes, and snippets.

@dextervip
Created July 17, 2020 01:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dextervip/995fdcd59caf1dcf004565447de36ab4 to your computer and use it in GitHub Desktop.
Save dextervip/995fdcd59caf1dcf004565447de36ab4 to your computer and use it in GitHub Desktop.
Auto decode DateTime Objects in json_decode PHP
<?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