Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a json decoded array with php?

Tags:

json

php

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?

like image 934
aurel Avatar asked Mar 11 '26 10:03

aurel


1 Answers

$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 />";
}
like image 114
genesis Avatar answered Mar 13 '26 22:03

genesis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!