【问题标题】:PHP: calculate array values by arrayPHP:按数组计算数组值
【发布时间】:2014-12-29 09:17:04
【问题描述】:

这对你们中的一些人来说可能很简单,但我在 php 中真的很笨。我所需要的只是转换一个多维数组并计算它的一些子数组值。

  Array
    (
        [0] => Array
            (
                [date] => 2014-10-30
                [mission] => one
                [point] => 10
            )

        [1] => Array
            (
                [date] => 2014-10-31
                [mission] => five
                [point] => 10
            )

        [2] => Array
            (
                [date] => 2014-11-19
                [mission] => one
                [point] => 8
            )

输出会是这样的:

 Array
(
    [one] => Array
        (
            [mission] => one
            [point] => 18 // sum all points where [mission] => one
            [count] => 2
            [everage] => 9 // 18/2
        )

    [five] => Array
        (
            [mission] => five
            [point] => 10
            [count] => 1
            [everage] => 10
        )

使用foreach 获取[point] 值的总和很简单,但是当我尝试获取具有相同[mission] 值的数组的count 时,麻烦就开始了。这是我的代码:

foreach($missionsarray as $row) {
  if(!isset($newarray[ $row['mission'] ])) {
       $newarray[ $row['mission'] ] = $row;
       $newarray[ $row['mission'] ]['count'] = count($row['point']);
       continue ;
   }
   $newarray[ $row['mission'] ]['point'] += $row['point'];
}

print_r($newarray);

【问题讨论】:

    标签: php arrays foreach count sum


    【解决方案1】:

    count() 的参数应该是一个数组,它返回该数组中元素的数量。那不是你想要的。首次在任务结果中创建新条目时,应将 count 设置为 1。然后,每次向其添加另一个 point 时,您只需递增它。

    foreach($missionsarray as $row) {
      if(!isset($newarray[ $row['mission'] ])) {
           $newarray[ $row['mission'] ] = $row;
           $newarray[ $row['mission'] ]['count'] = 1;
           continue ;
       }
       $newarray[ $row['mission'] ]['point'] += $row['point'];
       $newarray[ $row['mission'] ]['count']++;
    }
    
    print_r($newarray);
    

    【讨论】:

      【解决方案2】:

      使用这个

      $newarray[ $row['mission'] ]['point'] += $row['point'];
      $newarray[ $row['mission'] ]['count']++;
      
      print_r($newarray);
      

      PHP 5.5 更新

      array_column()

      $counts = array_count_values(array_flip(array_column($array, 'point')));
      

      【讨论】:

        【解决方案3】:

        首先,我认为这行给你带来了麻烦:

        $newarray[ $row['mission'] ]['count'] = count($row['point']);
        

        您应该将初始计数设置为 0,因为到目前为止您还没有看到任何记录:

        $newarray[ $row['mission'] ]['count'] = 0;
        

        在你这样做之后,每当你看到另一个具有相同任务的记录时,你可以像这样将一个添加到计数中:

        $newarray[ $row['mission'] ]['point'] += $row['point'];
        $newarray[ $row['mission'] ]['count']++;
        

        之后你应该可以计算平均值了。

        【讨论】:

          【解决方案4】:

          我认为您正在尝试执行以下操作:

          foreach ($missionsarray as $row) {
              $mission = $row['mission'];
              $point   = $row['point'];
          
              if (! isset($newarray[$mission])) {
                  $newarray[$mission] = [
                      'mission' => $mission,
                      'point'   => 0,
                      'count'   => 0,
                      'average' => 0
                  ];
              }
          
              $newarray[$mission]['point'] += $point;
              $newarray[$mission]['count']++;
          
              $newarray[$mission]['average'] =
                  $newarray[$mission]['point'] / $newarray[$mission]['count'];
          }
          

          【讨论】:

            【解决方案5】:

            我认为用for循环解决这个问题可能更容易,如下:

            <?php
            
            $arr = array(0 => array("date" => "2014-10-30","mission" => "one","point" => 10),
                         1 => array("date" => "2014-10-31","mission" => "five","point" => 10),
                         2 => array("date" => "2014-11-19","mission" => "one","point" => 8)
                         );
            
            $ototal = 0; // total mission one points
            $ftotal = 0; // total mission five points
            
            $ocount = 0; // count for mission one
            $fcount = 0; // count for mission five
            
            for ($i=0, $max = count( $arr ); $i < $max; $i++){
                if ($arr[$i]["mission"] == "one"){
                    $ototal += $arr[$i]["point"];
                    $ocount++;
            
                }
                else
                if ($arr[$i]["mission"] == "five"){
                    $ftotal += $arr[$i]["point"];
                    $fcount++;
                }
            }
            
            // transform $arr by adding the following elements:
            $arr["one"] = array("mission" => "one", "point" => $ototal, "count" => $ocount, "average" => $ototal/ $ocount);
            $arr["five"]= array("mission" => "five","point" => $ftotal, "count" => $fcount, "average" => $ftotal/$fcount);
            
            var_dump($arr["one"],$arr["five"]);
            

            现场演示here

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-12-29
              • 2021-12-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-03-21
              • 1970-01-01
              相关资源
              最近更新 更多