Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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代码所需的输出_Php_Arrays_Foreach - Fatal编程技术网

我没有得到以下PHP代码所需的输出

我没有得到以下PHP代码所需的输出,php,arrays,foreach,Php,Arrays,Foreach,我目前处于学习阶段 这是我的密码: <?php $food=array('Healthy'=> array('Pasta'=>200,'Vegetables'=>300,'Salad'=>100), 'Unhealthy'=> array('Pizza','Ice Cream')); foreach($food

我目前处于学习阶段

这是我的密码:

 <?php
    $food=array('Healthy'=>
                        array('Pasta'=>200,'Vegetables'=>300,'Salad'=>100), 

                'Unhealthy'=>
                        array('Pizza','Ice Cream'));
    foreach($food as $element =>$inner_array)   
        echo '<strong>'.$element.'</strong> <br>';
        foreach($inner_array as $inner_element)
        print_r ($inner_element.'<br>');
  ?>

我希望它显示与内部数组中元素相关的数字。

两个foreach循环中都缺少括号,在foreach执行后的第一行没有括号:

$food=array('Healthy'=> array('Pasta'=>200,'Vegetables'=>300,'Salad'=>100), 
            'Unhealthy'=> array('Pizza'=>0,'Ice Cream'=>1));

foreach($food as $element =>$inner_array) {  
    echo '<strong>'.$element.'</strong> <br>';
    foreach($inner_array as $inner_key => $inner_val) {
        print_r ($inner_val.'<br>');
    }
}
$food=array('health'=>array('意大利面=>200,'vegets'=>300,'salar'=>100),
'不健康'=>数组('Pizza'=>0,'Ice Cream'=>1));
foreach($food as$element=>$internal_数组){
回显“”.$element.
”; foreach($inner\u数组作为$inner\u键=>$inner\u val){ 打印($inner\u val.
); } }
两个foreach循环中都缺少方括号,没有方括号,只有在foreach执行后的第一行:

$food=array('Healthy'=> array('Pasta'=>200,'Vegetables'=>300,'Salad'=>100), 
            'Unhealthy'=> array('Pizza'=>0,'Ice Cream'=>1));

foreach($food as $element =>$inner_array) {  
    echo '<strong>'.$element.'</strong> <br>';
    foreach($inner_array as $inner_key => $inner_val) {
        print_r ($inner_val.'<br>');
    }
}
$food=array('health'=>array('意大利面=>200,'vegets'=>300,'salar'=>100),
'不健康'=>数组('Pizza'=>0,'Ice Cream'=>1));
foreach($food as$element=>$internal_数组){
回显“”.$element.
”; foreach($inner\u数组作为$inner\u键=>$inner\u val){ 打印($inner\u val.
); } }

这给出了我期望的输出。



这给出了我所期望的输出。

@Nik0333:这就是为什么您应该总是添加
{}
。虽然你可以省略它们,但你知道为什么它并不总是最好的主意。@Nik0333:这就是为什么你应该总是添加
{}
。虽然你可以省略它们,但你知道为什么它并不总是最好的主意。
<?php
    $food=array('Healthy'=> 
                            array('Pasta'=>200,'Vegetables'=>300,'Salad'=>100), 
                'Unhealthy'=> 
                            array('Pizza'=>500,'Ice Cream'=>350));     //Changes made here

    foreach($food as $element =>$inner_array) {

        echo '<strong>'.$element.'</strong> <br>';

    foreach($inner_array as $inner_key => $inner_val)    //Changes made here

        print_r ("$inner_key => $inner_val <br>");       //Changes made here

        }
    ?>