开发者

counting the array data

开发者 https://www.devze.com 2023-02-19 06:52 出处:网络
I need have 4 associative arrays like this with the same 4 users Array ( [0] => Array ( [userName] => jim [count] => 6 ) [1] => Array ( [userName] => joe [count] => 6 ) [2] => A

I need have 4 associative arrays like this with the same 4 users

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 ( [u开发者_JAVA百科serName] => mark [count] => 16 ) ) 


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


Array ( )

lets just say they are named

$values1
$values2
$values3
$values4

they may have all 4 users and they may also not have any or just some

I need to total all the values and have one array like this

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

is there an easy way to add these values and contrust one array like this with all the added data

I am sure i can figure it out the brute force way but i was wondering if there is a clean way of doing this


Here's a third alternative, encapsulated in to function format. ;-)

// 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


The easiest way would be to create an array with this structure:

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

This array can be created with a this loop:

$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'];
}

From this point, either use this structure directly or convert it to the format you want.


you can do this:

$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']; 
   }
}

This will give you a result like this:

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


I think this is what you want see it in action at 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消