【问题标题】:How to sums up values of a different key when same value is for another key in an associative array in PHP?当PHP中关联数组中的另一个键的值相同时,如何总结不同键的值?
【发布时间】:2016-12-26 10:29:41
【问题描述】:

请参考PHP中的以下数组:

$array1 = array(
    array('location'=>'AA','time_spent'=>2),
    array('location'=>'BB','time_spent'=>2),
    array('location'=>'AA','time_spent'=>3),
    array('location'=>'CC','time_spent'=>2),
    array('location'=>'CC','time_spent'=>2),
    array('location'=>'AA','time_spent'=>1)
);

在这里,我想计算他在每个特定位置花费的总时间,即如果 location 的键相同,则累积键 time_spent 的值。然后将其记录在一个新的关联数组中(比如$place_array),其值与键locationtotal_time 相关联。

所以输出数组应该是

$place_array = array(
    array('location'=>'AA','time_spent'=>6),
    array('location'=>'BB','time_spent'=>2),
    array('location'=>'CC','time_spent'=>4)
);

【问题讨论】:

  • 请更新您的问题,以包括您为达到预期结果所做的任何尝试(以及这些尝试的错误)
  • @Epodax,当然,我会确保在下一个问题中做到这一点。抱歉,我没有以前的尝试,因为我已经用给定的答案替换了。无论如何,感谢您的增量反馈。

标签: php key associative-array accumulate


【解决方案1】:
<?php

$array1 = array(
    array('location'=>'AA','time_spent'=>2),
    array('location'=>'BB','time_spent'=>2),
    array('location'=>'AA','time_spent'=>3),
    array('location'=>'CC','time_spent'=>2),
    array('location'=>'CC','time_spent'=>2),
    array('location'=>'AA','time_spent'=>1)
);

$place_array = array();
foreach ($array1 as $key => $value) {
    if(isset($place_array[$value['location']]))
        $place_array[$value['location']]['time_spent'] += $value['time_spent'];
    else
        $place_array[$value['location']] = $value;
}
$place_array = array_values($place_array);
echo '<pre>';
print_r($place_array);
echo '</pre>';
?>

输出

Array
(
    [0] => Array
    (
        [location] => AA
        [time_spent] => 6
    )

    [1] => Array
    (
        [location] => BB
        [time_spent] => 2
    )

    [2] => Array
    (
        [location] => CC
        [time_spent] => 4
    )
)

【讨论】:

    【解决方案2】:

    你可以试试这个。

    我创建了一个新数组$result,其键为位置,例如AA,在此我保存了位置为AA 的数组,下次我检查位置是否为@987654324 时花费的时间@ 然后将其花费的时间添加到上一个,如果没有,则添加新的。最后,我使用array_values 更改了密钥。

        <?php
        $array1 = array(array('location'=>'AA','time_spent'=>2),array('location'=>'BB','time_spent'=>2),array('location'=>'AA','time_spent'=>3),array('location'=>'CC','time_spent'=>2),array('location'=>'CC','time_spent'=>2),array('location'=>'AA','time_spent'=>1));
        $result = array();
        foreach($array1 as $new){
        if (array_key_exists($new['location'],$result)){
            $result[$new['location']]['time_spent'] += $new['time_spent'];
            }
        else{
            $result[$new['location']] = $new;
            }
        }
        $final = $new_array=array_values($result);
        echo "<pre>";print_r($final);
    

    输出

        Array
        (
            [0] => Array
                (
                    [location] => AA
                    [time_spent] => 6
                )
        
            [1] => Array
                (
                    [location] => BB
                    [time_spent] => 2
                )
        
            [2] => Array
                (
                    [location] => CC
                    [time_spent] => 4
                )
        
        )
    

    【讨论】:

      【解决方案3】:

      我的回答向您展示了我将如何获得您想要的数组,以及如果是我编码,我将使用的实际数组。如果我想要的只是位置和总时间的摘要,我会处理一个更紧凑的数组,不需要继续存储标签。无论如何,如果您不想要摘要位,如果您认为此版本更紧凑,请将其删除:)

          $journey = array(
          array('location'=>'AA','time_spent'=>2),
          array('location'=>'BB','time_spent'=>2),
          array('location'=>'AA','time_spent'=>3),
          array('location'=>'CC','time_spent'=>2),
          array('location'=>'CC','time_spent'=>2),
          array('location'=>'AA','time_spent'=>1)
      );
      
      $summary = Array();
      $full    = Array();
      foreach($journey as $map){
      
          // Your version
          if(!isset($full[$map["location"]])){
              $full[$map["location"]] = $map;
          } else {
              $full[$map["location"]]["time_spent"] += $map["time_spent"];
          }
      
          // Summary version (leaner)
          $summary[$map["location"]] = (isset($summary[$map["location"]])?$summary[$map["location"]]:0)+$map["time_spent"];
      }
      
      // Summary result
      print "<pre>";
      print_r($summary);
      print "</pre>";
      
      // Your result
      print "<pre>";
      print_r(array_values($full));
      print "</pre>";
      

      输出

      Array
      (
          [AA] => 6
          [BB] => 2
          [CC] => 4
      )
      
      Array
      (
          [0] => Array
              (
                  [location] => AA
                  [time_spent] => 6
              )
      
          [1] => Array
              (
                  [location] => BB
                  [time_spent] => 2
              )
      
          [2] => Array
              (
                  [location] => CC
                  [time_spent] => 4
              )
      
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-30
        • 2017-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多