If I understand you correctly, you should just be able to follow regular object syntax to get the result you want. Add the optional second parameter to json_decode
set to true
to get your json decoded as an associative array, as it seems as if this is the form that you're using it in.
$info = new stdClass();
$x = json_decode( $x, true );
foreach ( $x as $key => $val) {
$info->$key = $val;
}
As Ignas pointed out though, the results of json_decode()
already come back as a stdClass object, so if you just used $x = json_decode($x)
, you wouldn't need $info
at all... you'd already have $x
as a stdClass object.