计算数组数据

发布于 2024-10-26 09:52:05 字数 1057 浏览 4 评论 0原文

我需要有 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)
 );

数组一种添加这些值并将这样的一个数组与所有添加的数据相信任的简单方法

我确信我可以通过强力方式找出它,但我想知道是否有一种干净的方法可以做到这一点

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 ( [userName] => 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叹梦 2024-11-02 09:52:06

最简单的方法是创建具有此结构的数组:

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

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

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

从此时起,可以直接使用此结构或将其转换为所需的格式。

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.

清音悠歌 2024-11-02 09:52:06

你可以这样做:

$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)
 );

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)
 );
从来不烧饼 2024-11-02 09:52:06

我认为这就是您希望在 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);

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);
白云悠悠 2024-11-02 09:52:05

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

// 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);
}

演示

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文