Reputation: 20952
I have a JSON object that can be echoed to view in a browser as follows:
stdClass Object
(
[webmaster] => "data"
[analytics] => "data"
[facebook] => "data"
[twitter] => "data"
[maintenance] => 1
)
data are other values.
I get the above output using:
$data = json_decode($domainSpecific);
print_r($data);
what would be a good way to convert this JSON data into 5 variables preferably with the names of the JSON values - $webmaster, $analytics, $facebook, $twitter, $maintenance?
thankyou
Upvotes: 0
Views: 1149
Reputation: 1635
try this php extract()
$data = json_decode($domainSpecific, TRUE);
extract($data , EXTR_PREFIX_SAME);
edit
yes you need to do json_decode with true parameter to return as array
Upvotes: 1
Reputation: 32830
$data = extract(json_decode($domainSpecific, true));
print_r($data);
Upvotes: 1
Reputation: 9300
Though I'm not sure why would you do that and I'm also not sure if this is a good way to program, here you have something that works as I've tried:
class Test {
public $webmaster, $analytics, $facebook, $twitter, $manteinance;
}
$test = new Test();
$test->webmaster = 'Trololo';
$object_vars = get_object_vars($test);
foreach ($object_vars as $varname => $value) {
$$varname = $value;
}
echo $webmaster; //Trololo
Upvotes: 1
Reputation: 8863
$data = json_decode($domainSpecific);
foreach($data as $key=>$value)
{
$$key=$value;
}
Upvotes: 1