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

时间:2018-02-10 01:45:39

标签: php foreach while-loop each deprecated

由于每个()循环因 PHP 7.2 而被弃用,如何更新以下 while(()= each())循环没有$ value?

没有 $ 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;
  }
}

5 个答案:

答案 0 :(得分:22)

我找到了解决问题的方法,并考虑分享信息。以下是有关如何将each()循环升级到foreach()的其他案例。

案例1:缺少 $ value

reset($array);
while (list($key, ) = each($array)) {

更新至:

foreach(array_keys($array) as $key) {

案例2:缺少 $ key

reset($array);
while (list(, $value) = each($array)) {

更新至:

foreach($array as $value) {

案例3:不遗漏任何东西

reset($array);
while (list($key, $value) = each($array)) {

更新至:

foreach($array as $key => $value) {

答案 1 :(得分:1)

以下是一些方法:

标准foreach循环(非常易读):

foreach($this->contents as list($products_id)) {
    $total_items += $this->get_quantity($products_id);
}

或者,减少:

$total_items = array_reduce($this->contents, function($acc, $item) {
    return $acc + $this->get_quantity($products_id[0]);
});

或者,在函数表达式中:

$total_items = array_sum(array_map([$this, 'get_quantity'],
                         array_column($this->contents, 0)));

这些方法都不需要reset($this->contents);在它之前。

答案 2 :(得分:1)

要针对{strong>第3种情况,在Petro Mäntylä上给出出色的正确答案:

这里是“情况3”情况的完整示例,因为我发现完整的示例比一个行代码片段提供的信息更为丰富:

这是来自第三方旧代码库(TCPDF)的真实代码

已弃用:

shouldComponentUpdate(nextProps) {
    return (this.props.val !== nextProps.val);
}

已修复:

while (list($id, $name) = each($attr_array)) {
      $dom[$key]['attribute'][$name] = $attr_array[$id];
      ...              
      ...             
   }

答案 3 :(得分:0)

REPLACE THIS TO  BELOW ONE FOR DEPRECATED EACH FUNCTION IN CKFINDER CONFIG FILE

while (list($_key,$_resourceTypeNode) = each($GLOBALS['config']['ResourceType'])) {
//             if ($_resourceTypeNode['name'] === $resourceTypeName) {
//                 $this->_resourceTypeConfigCache[$resourceTypeName] = new CKFinder_Connector_Core_ResourceTypeConfig($_resourceTypeNode);

//                 return $this->_resourceTypeConfigCache[$resourceTypeName];
//             }
//         }

THIS ONE

foreach ($GLOBALS['config']['ResourceType'] as $key => $_resourceTypeNode) {
            if (isset($_resourceTypeNode['name'])) {
                if ($_resourceTypeNode['name'] === $resourceTypeName) {
                    $this->_resourceTypeConfigCache[$resourceTypeName] = new CKFinder_Connector_Core_ResourceTypeConfig($_resourceTypeNode);

                    return $this->_resourceTypeConfigCache[$resourceTypeName];
                }
            }
        }

答案 4 :(得分:-1)

 //  while (list($products_id, ) = each($this->contents)) {
   //  $total_items += $this->get_quantity($products_id);
 // }

更新至:

foreach(array_keys($this->contents) as $products_id) {
  $total_items += $this->get_quantity($products_id);
}

其他条件:

foreach($this->contents as $key =>$value) {
  $total_items += $this->get_quantity($products_id);
}