0

I have an array of order items, and I am collapsing the array so that duplicate lines are shown as only 1 line with " x 2" or " x 3" etc at the end of the line depending on the quantity.

The code is working but I am getting this warning/error:

Deprecated: The each() function is deprecated. This message will be suppressed on further calls...

How can I rewrite my function to work the same without using each()?

$order_lean=array_count_values($order);
$lean = array();
$str = "";
$first = 1;
while(list($key,$val) = each($order_lean)){ // <---
    array_push($lean, "$val x $key");
    if($first){
        $first = 0;
    }else{
        $str .= "\n";
    }
    $str .= "$val x $key";
}
return $str;
Gimme the 411
  • 994
  • 9
  • 25

1 Answers1

1

Just replace your while(list($key,$val) = each($order_lean)) { with:

foreach ($order_lean as $key => $val) {

Source: https://www.php.net/manual/en/control-structures.foreach.php

Robo Robok
  • 21,132
  • 17
  • 68
  • 126