Reputation: 3122
I have the following array and I can't figure out how to get any values out of it, this is the array:
Array
(
[0] => stdClass Object
(
[aure] => test
[menu] => stdClass Object
(
[pizza] => stdClass Object
(
[Tomato & Cheese] => stdClass Object
(
[small] => 5.50
[large] => 9.75
)
[onion] => stdClass Object
(
[small] => 5.50
[large] => 9.75
)
)
[Special Dinners] => stdClass Object
(
[Chicken Wings Dinner] => stdClass Object
(
[price] => 15.80
)
[onion] => stdClass Object
(
[small] => 5.50
[large] => 9.75
)
)
)
)
)
would you be able to give me an example of how can I get the price for a small Tomato & cheese pizza?
Upvotes: 3
Views: 193
Reputation: 50966
$array[0]->menu->pizza->{"Tomato & Cheese"}->small;
I have used curly brackets because I'm not able to get "Tomato & Cheese" without them (they have spaces)
This will give you all pizzas
$pizzas = (array) $array[0]->menu->pizza;
foreach($pizzas as $pizzaname => $choices){
echo $pizzaname." (small) is for ".$choices->small."<br />";
echo $pizzaname." (large) is for ".$choices->large."<br />";
}
Upvotes: 4
Reputation: 287755
If your names are not only alphanumeric, consider setting the assoc
parameter of json_decode
to true to get a nested dictionary instead of objects.
However, you can still access strange member names, using the following syntax:
echo 'Large t&c: ' . jsonArray[0]->menu->pizza->{'Tomato & Cheese'}->large;
Upvotes: 1