3

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?

2 Answers 2

4
$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 />";
}
Sign up to request clarification or add additional context in comments.

4 Comments

dam that looks easy :), thanks, is there a way to do it without knowing the names of pizzas
thanks @genesis, though the last bit of code, the foreach loop, is not showing anything.
this is the result of you $pizzas array : Array ( [Tomato & Cheese] => stdClass Object ( [small] => 5.50 [large] => 9.75 ) [onion] => stdClass Object ( [small] => 5.50 [large] => 9.75 ) )
@aurel: Edited. Execuse me for late response
1

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;

2 Comments

i will need to look into that "assoc" this as I dont know what that is. Is there a way to get the same results without knowing the name of "menu" and "pizza"?
@aurel Yup. Setting assoc to true allows you use all array functions, and you can simply iterate over them, for example in a nested for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.