简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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