开发者

how to slip this multi-dimensional array?

开发者 https://www.devze.com 2023-04-09 04:48 出处:网络
I have a nested array and I wanted to know if there is a way to slip it, so having the nested arrays as individual arrays

I have a nested array and I wanted to know if there is a way to slip it, so having the nested arrays as individual arrays

Array
(
    [0] => Array
        (
            [menu] => Array
                (
                    [pizza] => Array
                        (
                        开发者_Python百科    [Tomato & Cheese] => Array
                                (
                                    [small] => 5.50
                                    [large] => 9.75
                                )

                            [Olives] => Array
                                (
                                    [small] => 6.85
                                    [large] => 10.85
                                )

                        )

                    [Speciality Pizzas] => Array
                        (
                            [Our Special] => Array
                                (
                                    [ingredients] => Array
                                        (
                                            [0] => Tomatoes
                                            [1] => Olives
                                            [2] => Spinach
                                            [3] => Fresh Garlic
                                            [4] => Mozzarella & Feta Cheese
                                        )  --- theres more but you get the idea

Now I want to may a new array with all the pizzas, but without knowing the name "pizza"

at the moment I can do this:

$array = array(json_decode($json, true));
$pizzas = (array)$array[0]['menu']['pizza']

But if the menu changes content (but not structure) and if the 'pizza' changes to 'salads' the above would fail. Is the a way to create the above pizzas array without the name

Thanks


$array = array(json_decode($json, true));
$menu = (array)$array[0]['menu'];
foreach($menu as $item => $item_Data){
    //$item might be pizza for example
    //$item_Data might be Olives or Our special. Now you have to consider what to do with this. Maybe next foreach loop ?
}


Right now your array has parallel sections for related data. How about if you did something like:

$food_choices = array(
     'pizza' => array(
         'Tomato & Cheese' => array(
               'type' => 'regular',
               'prices' => array(...),
               'ingredients' => array(...)
         ),
         'Our Special' => array(
              'type' => 'specialty',
              'prices' => array(...),
              'ingredients' => array(...)
         ),
     ),
     'salads' => array(
          'Caesar' => array(...);
          'Tossed' => array(...);
     )
)

where all the information related to any one menu item as all in the same branch of the meu tree. Then to access any one pizza's information is as simple as:

$data = $food_choices['pizza']['Tomato & Cheese'];
echo 'The large of this pizza costs $', $data['prices']['large'];
echo 'The small Caesar salad contains ', implode($food_choices['salad']['Caesar']['ingredients);


A series of foreach loops might do, even though I don't know what you're doing.

<?php

    $pizza = '';

    foreach ($array as $food) {

        $pizza .= $food;

        if (is_array($food)) {

            foreach ($food as $option) {

                $pizza .= " > " . $option;

                if (is_array($option)) {

                    foreach ($option as $value) {

                        //etc
                    }
                }
            }
        }
    }

?>


To learn about the keys in an array, use the array_keys function (Demo):

$array = array(array('menu' => array(/* ... */))); # your array

print_r(array_keys($array[0])); # Array(menu)
0

精彩评论

暂无评论...
验证码 换一张
取 消