打印数组后foreach循环不起作用

时间:2016-11-11 06:39:20

标签: php arrays foreach

我试图循环使用以下数组。

Array
(
    [mech_info] => Array
        (
            [make] => Amaka
            [0] => Array
                (
                    [year] => 2001
                    [model] => Array
                        (
                            [0] => Test one
                            [1] => test fix
                            [2] => Hamour
                            [3] => Imagica
                        )    
                )

            [1] => Array
                (
                    [year] => 2002
                    [model] => Array
                        (
                            [0] => Test Two
                        )    
                )

            [2] => Array
                (
                    [year] => 2014
                    [model] => Array
                        (
                            [0] => Test three
                        )    
                )

            [3] => Array
                (
                    [year] => 2015
                    [model] => Array
                        (
                            [0] => test four
                        )    
                )    
        )    
)

Array
(
    [mech_info] => Array
        (
            [make] => PRI
            [0] => Array
                (
                    [year] => 2005
                    [model] => Array
                        (
                            [0] => PRIMODE
                            [1] => Temp Pri
                            [2] => primode
                            [3] => yyy
                        )    
                )    
        )    
)

我想用foreach循环来做。我尝试过以下代码但除了

之外没有显示任何内容
 `print_r($_POST['mech_show']);`.

       $all_make_model= $_POST['mech_show'];
        $all_make_model_data = $all_make_model['mech_info'];

    foreach ($all_make_model_data as $key => $mec_value) {
        echo "string";
        echo $meta_value['make'];
    }


    echo "<pre>";
    print_r($_POST['mech_show']);


    exit();

也无法进入foreach,数据无法循环打印。

给我错误

  

注意:未定义的索引:mech_info

Warning: Invalid argument supplied for foreach() in

我也是这样但是

$all_make_model= $_POST['mech_show'];
$all_make_model_data = $_POST['mech_info'];

    foreach ($all_make_model as $key => $mec_value) {
        echo "<pre>";
        print_r($mec_value['make']);
        echo "</pre>";
    }

    echo "<pre>";
    print_r($all_make_model['mech_info']);

但它是showing Warning: Illegal string offset 'mech_info' in ..

我不知道我的代码是错还是我错过了任何人帮助我。

谢谢

3 个答案:

答案 0 :(得分:0)

有些人会更改您的foreach循环。它$meta_value['make']应为$mec_value['make']

所以,

$all_make_model= $_POST['mech_show'];
//$all_make_model_data = $all_make_model['mech_info'];

foreach ($all_make_model as $key => $mec_value) {
    echo "<pre>";
    print_r($mec_value['make']);
    echo "</pre>";
}

答案 1 :(得分:0)

$all_array=array("mech_info"=>array("make"=>"Amaka",array("year"=>2001,"model"=>array("one","two","three")),array("year"=>2002,"model"=>array("one","two","three")),array("year"=>2003,"model"=>array("one","two","three")),array("year"=>2004,"model"=>array("one","two","three"))),array("mech_info"=>array("make"=>"PRI",array("year"=>2001,"model"=>array("one","two","three")))));

$all_make_model= $all_array;
//$all_make_model_data = $all_make_model['mech_info'];
//print_r($all_make_model['mech_info']);
foreach ($all_make_model['mech_info'] as $key => $mec_value) {
   if(is_numeric($key)) continue;
   echo $mec_value;    // output Amaka
}

exit();

答案 2 :(得分:0)

本准则正常运作。 为了迭代多个mech_info,我在数组中添加了一个leave,因为否则你要创建具有相同索引的多个对象。

$p = Array('mech_show' => Array(
    0 => Array(
        'mech_info' => Array(
                'make' => 'Amaka',
                '0' => Array(
                        'year' => 2001,
                        'model' => Array(
                                0 => 'Test one',
                                1 => 'test fix',
                                2 => 'Hamour',
                                3 => 'Imagica'
                            )    
                    ),

                '1' => Array(
                        'year' => 2002,
                        'model' => Array(
                                0 => 'Test Two'
                            )    
                    ),

                '2' => Array(
                        'year' => 2014,
                        'model' => Array(
                                0 => 'Test three'
                            )    
                    ),

                '3' => Array(
                        'year' => 2015,
                        'model' => Array
                            (
                                0 => 'test four'
                            )    
                    )    
            )    
        ),
    1=>Array(
        'mech_info' => Array(
                'make' => 'PRI',
                '0' => Array(
                        'year' => 2005,
                        'model' => Array(
                                0 => 'PRIMODE',
                                1 => 'Temp Pri',
                                2 => 'primode',
                                3 => 'yyy'
                            )    
                    )    
            )    
        )
    )
);

$all_make_model= $p['mech_show'];

foreach($all_make_model as $all_make_model_data){
    foreach($all_make_model_data as $mech_info)
    var_dump($mech_info['make']);
}

你必须用$ _POST

替换$ p