0

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

RC.
  • 13
  • 1
  • you need to show us what you tried and explain the exact problem you faced. "I tried something vague and it didn't work" is not a useful problem description. See [ask] and how to make a [mre] of the issue...don't just expect us to rewrite it for you from the beginning, that's not generally what we do (although you might get lucky if it's a small piece of work). – ADyson Jan 26 '23 at 12:15
  • 2
    See also general answers such as this https://stackoverflow.com/questions/46492621/how-can-i-update-code-that-uses-the-deprecated-each-function which should point you in the right direction. – ADyson Jan 26 '23 at 12:16
  • Thank you @ADyson, I'm new in this community so I don't have all the rules clear, however I appreciate your time answering, I'll delete my post and re organize it and I'll post it back :) Thanks again! – RC. Jan 26 '23 at 12:23

2 Answers2

1

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>";    
}
Juan
  • 690
  • 4
  • 15
  • My answer was accepted but also refused apparently...? I would like to know why it was refused. So as not to make the same mistake next time. And don't post an answer if it's not helpful. – Juan Jan 31 '23 at 11:20
0

for a more elegant solution and better optimisation you can use foreach and a switch statement

foreach ($_SESSION['cart'][$i] as $key => $val) {
    switch ($key) {
        case "product_name":
            $product_name = $val;
            break;
        case "units":
            $units = $val;
            break;
        case "packing_size":
            $packing_size = $val;
            break;
        case "grade":
            $grade = $val;
            break;
        case "length":
            $length = $val;
            break;
        case "quantity":
            $quantity = $val;
            break;
    }
}
echo $product_name . "" . $units . "" . $packing_size . "" . $grade . "" . $length . "" . $quantity;
echo "<be>";
Terchila Marian
  • 2,225
  • 3
  • 25
  • 48
  • The switch is more elegant than several ifs we agree but why carry out tests with swicth (or several ifs) when we can access the values ​​via the keys...? – Juan Jan 27 '23 at 16:58