代码之家  ›  专栏  ›  技术社区  ›  oliverbj

PHP-For each循环-如果值相同(但仍然是和值),则跳过

  •  0
  • oliverbj  · 技术社区  · 6 年前

    我有以下循环:

     $Sum = '';
     $CommodityCode = '';
         foreach ($obj['statisticalvalues'] as $key => $value) {
             if (isset($obj['commoditycodes'][$key]['match'])) {
                 //If our commodity code from last iteration is the same as current = we have a duplicate!
                 if ($CommodityCode !== $obj['commoditycodes'][$key]['match']) {
                      $Sum = $value['key_0'];
                  } else {
                      $Sum += $value['key_0'];
                  }
    
                  $ReturnString .= '' . $obj['commoditycodes'][$key]['match'] . '';
                  $ReturnString .= '' . $Sum . '';
    
                  //Set the code for next iteration
                  $CommodityCode = $obj['commoditycodes'][$key]['match'];
                }
            }
    

    CODE        VALUE
    3002190000  610.20
    3002190000  846.7
    3002190000  1083.2
    3002190000  4156.4
    3002190000  4461.5
    3002190000  4711.4
    3002190000  5061.4
    3002190000  6061
    3002190000  6886.3
    3002190000  7136.2
    3002190000  7435.8
    3002190000  8196
    3002190000  8466.2
    8000120000  1000.5
    

    如你所见 VALUE 正确求和,但它仍然打印出 CODE 每行。

    CODE        VALUE
    3002190000  8466.2
    8000120000  1000.5
    
    4 回复  |  直到 6 年前
        1
  •  3
  •   Muttley    6 年前

    假设 $input 'CODE' 'VALUE' ,我们可以使用 $output 数组:

    $output = array();
    foreach($input as $row)
    {
        $key = $row['CODE'];
        $value = $row['VALUE'];
        $output[$key] = isset($output[$key]) ? $output[$key] + $value : $value;
    }
    print_r($output);
    

    请注意,上述解决方案会导致 $输出 数组动态增长,这对于大型数组来说可能是一个(时间)问题。如果是这种情况,请考虑预先计算 $input[$i]['CODE'] $i=1 ... count($input) 以便您可以预先分配 使用数组 array_pad .

        2
  •  0
  •   Oliver M Grech    6 年前

    使用php的 array_unique 如果你想要不同的值。

    钥匙总是不同的,所以你不必担心。

    <?php
    
     $newArrayDistinct = array_unique($obj['statisticalvalues']);
    
     foreach(newArrayDistinct as $key=>$value){
    
          // your code here.
    
     }
    

    如果你想用你当前的代码,你可以用这个。。。

     $Sum = '';
     $CommodityCode = '';
    
    $obj['statisticalvalues'] = array_unique($obj['statisticalvalues']);
         foreach ($obj['statisticalvalues'] as $key => $value) {
             if (isset($obj['commoditycodes'][$key]['match'])) {
                 //If our commodity code from last iteration is the same as current = we have a duplicate!
                 if ($CommodityCode !== $obj['commoditycodes'][$key]['match']) {
                      $Sum = $value['key_0'];
                  } else {
                      $Sum += $value['key_0'];
                  }
    
                  $ReturnString .= '' . $obj['commoditycodes'][$key]['match'] . '';
                  $ReturnString .= '' . $Sum . '';
    
                  //Set the code for next iteration
                  $CommodityCode = $obj['commoditycodes'][$key]['match'];
                }
            }
    
        3
  •  0
  •   Nigel Ren    6 年前

    $Sum = '';
    $CommodityCode = '';
    foreach ($obj['statisticalvalues'] as $key => $value) {
        if (isset($obj['commoditycodes'][$key]['match'])) {
            //If our commodity code from last iteration is the same as current = we have a duplicate!
           if ($CommodityCode !== $obj['commoditycodes'][$key]['match']) {
               if ( $CommodityCode != '' ) {
                   $ReturnString .= '' . $obj['commoditycodes'][$key]['match'] . '';
                   $ReturnString .= '' . $Sum . '';
               }
               $Sum = $value['key_0'];
           } else {
               $Sum += $value['key_0'];
           }
    
           //Set the code for next iteration
           $CommodityCode = $obj['commoditycodes'][$key]['match'];
        }
    }
    if ( $CommodityCode != '' ) {
        $ReturnString .= '' . $obj['commoditycodes'][$key]['match'] . '';
        $ReturnString .= '' . $Sum . '';
    }
    

    (我没有数据,所以无法测试,但希望可以)

        4
  •  0
  •   Atmahadli    6 年前

    $input = array(
      array('code'=>'ID001', 'value'=>100),
      array('code'=>'ID001', 'value'=>78),
      array('code'=>'ID002', 'value'=>123),
      array('code'=>'ID002', 'value'=>104),
      array('code'=>'ID001', 'value'=>119),
      array('code'=>'ID001', 'value'=>93),
      array('code'=>'ID003', 'value'=>55),
    );
    
    //process
    $output=array();
    foreach($input as $item){
      if(isset($output[$item['code']])){
        $output[$item['code']] += $item['value'];
      }else{
        $output[$item['code']] = $item['value'];
      }
    }
    
    //print output
    echo "CODE : VALUE";
    foreach($output as $code=>$value){
      echo "<br>$code : $value";
    }