不推荐使用PHP 7.2:while = every()循环没有$ value

问题描述 投票:6回答:3

由于每个()循环从PHP 7.2开始不推荐使用,如何在没有$ value的情况下更新下面的while((= = each())循环?

如果没有$ value,我就无法使foreach循环工作。另外,while($ products_id = $ this-> contents)导致无限循环。

谢谢!

function count_contents() {  // get total number of items in cart 
  $total_items = 0;
  if (is_array($this->contents)) {
    reset($this->contents);
    while (list($products_id, ) = each($this->contents)) {
      $total_items += $this->get_quantity($products_id);
    }
  }
  return $total_items;
}
function get_quantity($products_id) {
  if (isset($this->contents[$products_id])) {
    return $this->contents[$products_id]['qty'];
  } else {
    return 0;
  }
}
php foreach while-loop each deprecated
3个回答
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.