Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 嵌套的Foreach循环显示-警告:为Foreach()提供的参数无效_Php_Arrays_Foreach - Fatal编程技术网

Php 嵌套的Foreach循环显示-警告:为Foreach()提供的参数无效

Php 嵌套的Foreach循环显示-警告:为Foreach()提供的参数无效,php,arrays,foreach,Php,Arrays,Foreach,我有一个包含多个数据的数组。现在我想从中检索数据。 此数组存储在$restaurant\u菜单中 排列 我想从中显示餐厅菜单。菜单有一个组,如汤。有几种汤。因此我想获得如下信息: --------------------- 等 ------------------- 我一直在用这个密码 但错误显示:警告:为foreach()提供的参数无效。 当使用$restaurant\u menu\u items作为参数时,第三个foreach循环很幸运在尝试访问和处理数组的成员之前,您需要检查每个阶段处理数

我有一个包含多个数据的数组。现在我想从中检索数据。 此数组存储在
$restaurant\u菜单中

排列 我想从中显示餐厅菜单。菜单有一个组,如
。有几种汤。因此我想获得如下信息:

--------------------- 等

------------------- 我一直在用这个密码 但错误显示:
警告:为foreach()提供的参数无效。

当使用
$restaurant\u menu\u items
作为参数时,第三个foreach循环很幸运

在尝试访问和处理数组的成员之前,您需要检查每个阶段处理数组的情况,并且要这样做,请使用
is\u array

$menus = $restaurant_menus;

if( is_array( $menus ) && !empty( $menus ) ){
    foreach( $menus as $names ) {

        array_shift( $names );

        if( is_array( $names ) && !empty( $names ) ){
            foreach( $names as $menu ) {

                if( is_array( $menu ) && !empty( $menu ) ){
                    foreach( $menu as $items ) {

                        if( is_array( $items )  && !empty( $items ) ){
                            foreach( $items as $item ) {

                                print_r( $item );

                            }
                        }
                    }
                }
            }
        }
    }
}


在尝试访问和处理数组的成员之前,您需要检查在处理数组的每个阶段,并使用
is\u array
这样做

$menus = $restaurant_menus;

if( is_array( $menus ) && !empty( $menus ) ){
    foreach( $menus as $names ) {

        array_shift( $names );

        if( is_array( $names ) && !empty( $names ) ){
            foreach( $names as $menu ) {

                if( is_array( $menu ) && !empty( $menu ) ){
                    foreach( $menu as $items ) {

                        if( is_array( $items )  && !empty( $items ) ){
                            foreach( $items as $item ) {

                                print_r( $item );

                            }
                        }
                    }
                }
            }
        }
    }
}


在foreach()内部调用之前,在每个步骤中使用is_数组($restaurant_menu)。这是因为foreach需要一个数组作为参数adn not stringUse在foreach()内部调用它之前,在每一步都使用is_数组($restaurant_菜单)。发生这种情况的原因是foreach需要一个数组作为参数,而不是StringNot same problem:(同样的问题:(
foreach ($restaurant_menus as $restaurant_menu_names) {
    array_shift($restaurant_menu_names);
    foreach ($restaurant_menu_names as $restaurant_menu_name) {
        foreach ($restaurant_menu_name as $restaurant_menu_items) {
            foreach ($restaurant_menu_items as $restaurant_menu_item) {
                print_r($restaurant_menu_item);
            }
        }
    }
}
$menus = $restaurant_menus;

if( is_array( $menus ) && !empty( $menus ) ){
    foreach( $menus as $names ) {

        array_shift( $names );

        if( is_array( $names ) && !empty( $names ) ){
            foreach( $names as $menu ) {

                if( is_array( $menu ) && !empty( $menu ) ){
                    foreach( $menu as $items ) {

                        if( is_array( $items )  && !empty( $items ) ){
                            foreach( $items as $item ) {

                                print_r( $item );

                            }
                        }
                    }
                }
            }
        }
    }
}
$menus=array(
    array(
        'menu_group_name'=>'soup',
        'menu_group_cover_photo'=>array('430','431'),
        'menu_group_single_menu'=>array(
                array(
                        'single_menu_name'=>'Miryala Rasam',
                        'single_menu_desc'=>'Pepper Rasam Masala Soup',
                        '_price'=>7.99
                    ),
                    array(
                        'single_menu_name'=>'Mokkajonna Shorba',
                        'single_menu_desc'=>'Thick sweet corn soup',
                        '_price'=>8.99
                    )
            )
        )
);
if( is_array( $menus ) && !empty( $menus ) ){
    foreach( $menus as $names ) {

        array_shift( $names );

        if( is_array( $names ) && !empty( $names ) ){
            foreach( $names as $menu ) {

                if( is_array( $menu ) && !empty( $menu ) ){
                    foreach( $menu as $items ) {

                        if( is_array( $items )  && !empty( $items ) ){
                            foreach( $items as $item ) {

                                print_r( $item );

                            }
                        }
                    }
                }
            }
        }
    }
}

/*
    output:
    -------
    Miryala Rasam
    Pepper Rasam Masala Soup
    7.99
    Mokkajonna Shorba
    Thick sweet corn soup
    8.99
*/