Skip to content

Instantly share code, notes, and snippets.

@mgng
Created July 11, 2012 07:01
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 mgng/3088541 to your computer and use it in GitHub Desktop.
Save mgng/3088541 to your computer and use it in GitHub Desktop.
<?php
function obj( $json_str = null ) {
$obj = new stdClass();
$json = json_decode( $json_str );
if ( $json !== null ) {
foreach( $json as $k => $v ) {
$obj->{"{$k}"} = $v;
}
}
return $obj;
}
//--------------------
$obj1 = obj();
$obj2 = obj('{
"name" : "ムグお",
"id" : "mgng",
"sites" : [
"https://twitter.com/mgng",
"http://mgngoccult.tumblr.com/"
]
}');
print_r( $obj1 );
print_r( $obj2 );
stdClass Object
(
)
stdClass Object
(
[name] => ムグお
[id] => mgng
[sites] => Array
(
[0] => https://twitter.com/mgng
[1] => http://mgngoccult.tumblr.com/
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment