• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing a found function or combining arrays

#1
I found a function on google (that worx well), but i'm not good enough to change it so that it does something  slightly different

Code:
                    $data['total_summed'] = array_reduce($data['dates'], function ($a, $b) {
                       isset($a[$b['ct_id']]) ? $a[$b['ct_id']]['total_cost'] += $b['total_cost'] : $a[$b['ct_id']] = $b;  
                       return $a;
                   });

what it returns is =
Array ( [227] => Array ( [ct_id] => 227 [support_item] => Assistance to access community, social and recreational activities - individual - per weekdays
[hours] => 6.00 [amount] => 35.00 [total_cost] => 210 [cluster_tasks] => Array ( [svce_num] => 23 [task_num] => 008 ) )
[228] => Array ( [ct_id] => 228 [support_item] => House cleaning and other household activities
[hours] => 3.00 [amount] => 34.56 [total_cost] => 207.36 [cluster_tasks] => Array ( [svce_num] => 20 [task_num] => 002 ) )
[229] => Array ( [ct_id] => 229 [support_item] => Other transport fares
[hours] => 1.00 [amount] => 54.86 [total_cost] => 54.86 [cluster_tasks] => Array ( [svce_num] => 10 [task_num] => 001 ) )
[230] => Array ( [ct_id] => 230 [support_item] => Personal training
[hours] => 1.00 [amount] => 40.00 [total_cost] => 360 [cluster_tasks] => Array ( [svce_num] => 25 [task_num] => 008 ) )
[231] => Array ( [ct_id] => 231 [support_item] => Assistance with personal domestic activities
[hours] => 4.00 [amount] => 35.00 [total_cost] => 280 [cluster_tasks] => Array ( [svce_num] => 9 [task_num] => 004 ) ) )

which is correct, but I also want to add up the 'hours' at the same time, but dont know how to change the function?

I can write another one as

Code:
                    $data['total_summed1'] = array_reduce($data['dates'], function ($a, $b) {
                       isset($a[$b['ct_id']]) ? $a[$b['ct_id']]['hours'] += $b['hours'] : $a[$b['ct_id']] = $b;  
                       return $a;
                   });

and this rturns =
Array ( [227] => Array ( [ct_id] => 227 [support_item] => Assistance to access community, social and recreational activities - individual - per weekdays
[hours] => 6.00 [amount] => 35.00 [total_cost] => 210 [cluster_tasks] => Array ( [svce_num] => 23 [task_num] => 008 ) )
[228] => Array ( [ct_id] => 228 [support_item] => House cleaning and other household activities
[hours] => 6 [amount] => 34.56 [total_cost] => 103.68 [cluster_tasks] => Array ( [svce_num] => 20 [task_num] => 002 ) )
[229] => Array ( [ct_id] => 229 [support_item] => Other transport fares
[hours] => 1.00 [amount] => 54.86 [total_cost] => 54.86 [cluster_tasks] => Array ( [svce_num] => 10 [task_num] => 001 ) )
[230] => Array ( [ct_id] => 230 [support_item] => Personal training
[hours] => 9 [amount] => 40.00 [total_cost] => 40 [cluster_tasks] => Array ( [svce_num] => 25 [task_num] => 008 ) )
[231] => Array ( [ct_id] => 231 [support_item] => Assistance with personal domestic activities
[hours] => 8 [amount] => 35.00 [total_cost] => 140 [cluster_tasks] => Array ( [svce_num] => 9 [task_num] => 004 ) ) )
                   
but then I have 2 seperate arrays each with a portion correct. Would there be an elegant way of re-writing the function? or maybe a way to
combine the 2 arrays so that it comes out as (last row from each array as example) would look like =

[231] => Array ( [ct_id] => 231 [support_item] => Assistance with personal domestic activities
[hours] => 8 [amount] => 35.00 [total_cost] => 280 [cluster_tasks] => Array ( [svce_num] => 9 [task_num] => 004 ) ) )

have tried array_merge and array_combine but they didn't do this so maybe there's a different way?

thanks everyone
Reply

#2
You can try this:

PHP Code:
$data['total_summed'] = array_reduce($data['dates'], function ($a$b) {
   if (isset($a[$b['ct_id']])) {
       $a[$b['ct_id']]['total_cost'] += $b['total_cost'];
       $a[$b['ct_id']]['hours'] += $b['hours'];
   } else {
       $a[$b['ct_id']] = $b;
   }
   return $a;
}); 
Reply

#3
Yes, thanks so much! It works perfectly.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.