PHP:合并基于特定键和放大器数组元素;值 [英] PHP: merge elements of an array based on specific key & value

查看:107
本文介绍了PHP:合并基于特定键和放大器数组元素;值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个大约20个不同的想法,我想我已经走了这么多圈我不记得在那里我开始。我敢肯定,这是一个简单的了。

I've tried about 20 different ideas for this and I think I've gone in so many circles I can't remember where I started. I'm sure this is a simple one too.

我有一个数组;

Array
(
    [0] => Array
        (
            [employee] => 15
            [first_name] => Person1
            [surname] => Person1
            [totaltime] => 183.75
            [type] => 0

        )

    [1] => Array
        (
            [employee] => 15
            [first_name] => Person1
            [surname] => Person1
            [totaltime] => 64.50
            [type] => 1            
        )

    [2] => Array
        (
            [employee] => 23
            [first_name] => Person2
            [surname] => Person2
            [totaltime] => 2.00
            [type] => 0
        )

    [3] => Array
        (
            [employee] => 51
            [first_name] => Person3 
            [surname] => Person3
            [totaltime] => 119.25
            [type] => 0            
        )

    [4] => Array
        (
            [employee] => 59
            [first_name] => Person4
            [surname] => Person4
            [totaltime] => 170.75
            [type] => 0
        )
)

我需要合并其中员工数相同的元素,并添加 TOTALTIME 元素,以新的密钥根据键入。这就是我的意思。

I need to merge the elements where the employee number is the same and add the totaltime elements to new keys based on the type. This is what I mean.

Array
(
    [0] => Array
        (
            [employee] => 15
            [first_name] => Person1
            [surname] => Person1
            [totaltime_type_0] => 183.75
            [totaltime_type_1] => 64.50

        )       

    [1] => Array
        (
            [employee] => 23
            [first_name] => Person2
            [surname] => Person2
            [totaltime_type_0] => 2.00
            [totaltime_type_1] => 0
        )

    [2] => Array
        (
            [employee] => 51
            [first_name] => Person3 
            [surname] => Person3
            [totaltime_type_0] => 119.25
            [totaltime_type_1] => 0            
        )

    [3] => Array
        (
            [employee] => 59
            [first_name] => Person4
            [surname] => Person4
            [totaltime_type_0] => 170.75
            [totaltime_type_1] => 0
        )
)

我能做到这一点在查询级别,但创建原始数组的SQL是如此复杂,我宁愿只能维持它的一个版本。

I could do this at the query level but the SQL to create the original array is so complex I'd rather only maintain the one version of it.

这是最好的办法任何建议,真正帮助我重回正轨与此有关。并且将非常AP preciated。

Any suggestions on the best approach would really help me get back on track with this. And will be REALLY appreciated.

推荐答案

试试这个:

$res      = array();
foreach($your_array as $val){
   $res[$val['employee']]['employee']         = $val['employee'];
   $res[$val['employee']]['first_name']       = $val['first_name'];
   $res[$val['employee']]['surname']          = $val['surname'];
   if($val['type'] == 0){
      if(array_key_exists("totaltime_type_0",$res[$val['employee']])){
         $res[$val['employee']]['totaltime_type_0'] += $val['totaltime'];
      }
      else{
         $res[$val['employee']]['totaltime_type_0'] = $val['totaltime'];
      }
   }
   if($val['type'] == 1){
      if(array_key_exists("totaltime_type_1",$res[$val['employee']])){
         $res[$val['employee']]['totaltime_type_1'] += $val['totaltime'];
      }
      else{
         $res[$val['employee']]['totaltime_type_1'] = $val['totaltime'];
      }
   }
}

echo "<pre>";
print_r(array_values($res));

这篇关于PHP:合并基于特定键和放大器数组元素;值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆