Thursday, 30 August 2018

PHP - foreach loop does not work after the print array

I've tried to loop of following array.

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
                        )
                )
        )
)

I want to do it with foreach loop. I have tried by following code but it is not show anything except
 `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();

also not able to go under foreach and data not print in loop.
given me error
Notice: Undefined index: mech_info
Warning: Invalid argument supplied for foreach() in

i also trie this way but
$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']);

but it's showing Warning: Illegal string offset 'mech_info' in ..
I don't know if my code is wrong or I'm missing something anyone pls help me.
Thank You

Some change your foreach loop. it $meta_value['make'] should be $mec_value['make']
So,
$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>";
}

0 comments:

Post a Comment