changing a found function or combining arrays - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: changing a found function or combining arrays (/showthread.php?tid=62416) |
changing a found function or combining arrays - scoobie - 07-12-2015 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) { 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) { 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 RE: changing a found function or combining arrays - pdthinh - 07-12-2015 You can try this: PHP Code: $data['total_summed'] = array_reduce($data['dates'], function ($a, $b) { RE: changing a found function or combining arrays - scoobie - 07-13-2015 Yes, thanks so much! It works perfectly. |