计算数组数据

时间:2011-03-24 15:48:44

标签: php arrays count associative-array

我需要4个相关联的数组,使用相同的4个用户

Array ( [0] => Array ( [userName] => jim [count] => 6 ) [1] => Array ( [userName] => joe [count] => 6 ) [2] => Array ( [userName] => jesse [count] => 36 ) [3] => Array ( [userName] => mark [count] => 2 ) ) 


Array ( [0] => Array ( [userName] => jim [count] => 2 ) [1] => Array ( [userName] => jesse [count] => 2 ) [2] => Array ( [userName] => mark [count] => 16 ) ) 


Array ( [0] => Array ( [userName] => jim [count] => 8 ) ) 


Array ( )

我们只能说它们被命名为

$values1
$values2
$values3
$values4

他们可能拥有所有4个用户,他们可能也没有任何或只有一些

我需要总计所有值,并且有一个像这样的数组

 $people = array(
    array("name" => "jim", "score" => 33), 
    array("name" => "jesse", "score" => 44),
    array("name" => "mark", "score" => 66),
    array("name" => "joe", "score" => 11)
 );

有一种简单的方法可以添加这些值并使用所有添加的数据来控制这样的数组

我确信我可以用蛮力的方式解决这个问题,但我想知道是否有一种干净的方法可以做到这一点

4 个答案:

答案 0 :(得分:2)

这是第三种替代方案,封装在函数格式中。 ; - )

// tally_counts($score1, $scores2[, $score3[, ...]])
function tally_counts(){
  $result = array();
  foreach (func_get_args() as $counts){
    foreach ($counts as $count){
      if (!array_key_exists($count['userName'],$result)){
        $result[$count['userName']] = array(
          'name'  => $count['userName'],
          'count' => $count['count']
        );
      }else{
        $result[$count['userName']]['count'] += $count['count'];
      }
    }
  }
  return array_values($result);
}

<强> DEMO

答案 1 :(得分:1)

最简单的方法是创建一个具有这种结构的数组:

$people = array(
    'jim' => 33,
    'jess' => 44
    // ...
);

可以使用此循环创建此数组:

$people = array();

foreach(array_merge($values1, $values2, $values3, $values4) as $person) {
    $name = $person['name'];
    if(!isset($people[$name])) {
        $people[$name] = 0;
    }
    $people[$name] += $person['score'];
}

从这一点开始,直接使用此结构或将其转换为您想要的格式。

答案 2 :(得分:1)

你可以这样做:

$people = array();

foreach(array_merge($values1, $values2, $values3, $values4) as $k=>$v){
   if(isset($people[$v['username']]){
        $people[$v['username']]['score'] += $v['count'];
   }
   else {
       $people[$v['username']] = array();
       $people[$v['username']]['score'] = $v['count']; 
   }
}

这会给你一个这样的结果:

$people = array(
    "jim" => array("score" => 33), 
    "jesse" => array("score" => 44),
    "mark" => array("score" => 66),
    "joe" => array("score" => 11)
 );

答案 3 :(得分:1)

我认为这是你想要的http://www.oriontechnologysolutions.com/stackoverflow/arraycount.php

$list1 = Array (Array ( 'userName' => 'jim', 'count' => 6 ),Array ( 'userName' => 'joe' , 'count' => 6 ), Array ( 'userName' => 'jesse', 'count' => 36 ), Array ( 'userName' => 'mark' , 'count' => 2 ) ) ;
$list2 = Array (Array ( 'userName' => 'jim' , 'count' => 2 ), Array ( 'userName' => 'jesse' , 'count' => 2 ), Array ( 'userName' => 'mark' , 'count' => 16 ) ) ;
$list3 = Array (Array ( 'userName' => 'jim', 'count' => 8 ) ) ;
$list4 = Array ( );


$master_list = Array($list1, $list2, $list3, $list4);
$final = Array();
foreach($master_list as $glist) {
        foreach($glist as $person) {
                if(!isset($final[$person['userName']]))
                        $final[$person['userName']] = 0;
                $final[$person['userName']] += $person['count'];
        }
}
print_r($final);