Skip to content

Instantly share code, notes, and snippets.

@eftakhairul
Created September 3, 2012 20:49
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 eftakhairul/3613354 to your computer and use it in GitHub Desktop.
Save eftakhairul/3613354 to your computer and use it in GitHub Desktop.
Json to function call
<?php
$json = 'YOUR JOSN HERE';
$patterns = json_decode($json);
$patternObjects = array();
foreach ($patterns->patterns as $pattern) {
$patternObj = new Pattern();
$patternObjects[] = mapJsonToObject($patternObj, $pattern);
}
var_dump($patternObjects);
function mapJsonToObject($object, $json)
{
$properties = get_object_vars($json);
foreach ($properties as $property => $value) {
if (!is_array($value) && !is_object($value)) {
$setter = "set" . ucfirst($property);
$object->$setter($json->$property);
}
}
return $object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment