简体   繁体   English

PHP:根据特定的键和值合并数组的元素

[英]PHP: merge elements of an array based on specific key & value

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. 为此,我尝试了20种不同的想法,但我想我已经走了很多圈,以至于我都不记得我从哪里开始。 I'm sure this is a simple one too. 我敢肯定这也是一个简单的方法。

I have an array; 我有一个数组;

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

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 . 我需要合并employee人数相同的元素,并根据typetotaltime元素添加到新键中。 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
        )
)

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. 我可以在查询级别执行此操作,但是创建原始数组的SQL非常复杂,我只希望维护它的一个版本。

Any suggestions on the best approach would really help me get back on track with this. 关于最佳方法的任何建议都将真正帮助我重回正轨。 And will be REALLY appreciated. 并且将不胜感激。

Try this : 尝试这个 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM