我有一个包含多个数据的数组。现在我想从中检索数据。
该数组存储在$restaurant_menus
Array
(
[0] => Array
(
[menu_group_name] => Soup
[menu_group_cover_photo] => Array
(
[0] => 430
[1] => 431
)
[menu_group_single_menu] => Array
(
[0] => Array
(
[single_menu_name] => Miryala Rasam
[single_menu_desc] => Pepper Rasam Masala Soup
[_price] => 7.99
)
[1] => Array
(
[single_menu_name] => Mokkajonna Shorba
[single_menu_desc] => Thick sweet corn soup
[_price] => 8.99
)
)
)
)
我想从它显示餐馆菜单.Menu有一个像soup
这样的小组。有几种汤。所以我想得到这样的信息:
-- Soup
-- Cover Photo
|----- Single menu -- Price
|---------------------------------
|----- Miryala Rasam ---7.99
|----- Mokkajonna Shorba --- 8.99
等
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);
}
}
}
}
但错误显示:Warning: Invalid argument supplied for foreach()
当使用$restaurant_menu_items
作为参数
答案 0 :(得分:0)
在尝试访问和处理数组成员之前,您需要检查每个阶段是否正在处理数组 - 并且这样做会使用is_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 );
}
}
}
}
}
}
}
}
$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
*/