Skip to content

Instantly share code, notes, and snippets.

@mgng
Created Jul 11, 2012
Embed
What would you like to do?
<?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