Home > OS >  Uncaught Error: Call to undefined function each()
Uncaught Error: Call to undefined function each()

Time:01-26

I'm wondering if you guys can give me an idea about how to replace this each function:

while(list($key,$val) = each($_SESSION['cart'][$i]))
        {
            if ($key=="product_name"){
                $product_name=$val;
            }else if($key=="units"){
                $units=$val;
            }else if($key=="packing_size"){
                $packing_size=$val;
            }else if($key=="grade"){
                $grade=$val;
            }else if($key=="length"){
                $length=$val;
            }else if($key=="quantity"){
                $quantity=$val;
            }
        }
        echo $product_name."".$units."".$packing_size."".$grade."".$length."".$quantity;
        echo "<br>";
    }

Thanks!

I've try to use foreach, but isn't working at all

CodePudding user response:

I assume your original question is "How to replace a deprecated loop with each()"...?
In this case ADyson's commentary will be of great help to you.

But then why carry out tests on keys to obtain values ​​that you then place in variables that you only display...?
If you just want to display the values ​​you can access your array through the keys
As in the code below

foreach($_SESSION['cart'][$i] as $key => $val)
{
    echo $val["product_name"]."".$val["units"]."".$val["packing_size"]."".$val["grade"]."".$val["length"]."".$val["quantity"];
    echo "<br>";    
}
  • Related