I recently asked a question about,merging the values of arrays in an array of arrays in php and got the correct answer, how ever it came with problems, ones I cannot figure out regardless of the var dumps or echos I place.
Using the same array example, and the solution provided if I do:
$result = array();
if(is_array($ticketLabors) && !empty($ticketLabors)){
foreach ($ticketLabors as $innerArray) {
foreach ($innerArray as $key=>$value) {
$result[$key] = number_format($result[$key] + $value, 2);
}
}
}
var_dump($result);
On an array like such:
array(2) {
[0]=>
array(10) {
["ticket_labor_ot_travel_c"]=>
string(5) "34.50"
["ticket_labor_travel_c"]=>
string(5) "23.00"
["ticket_labor_ot_c"]=>
string(5) "34.50"
["ticket_labor_reg_c"]=>
string(5) "23.00"
["ticket_labor_user_id"]=>
string(3) "319"
["ticket_labor_tot_hours"]=>
string(4) "0.50"
["ticket_labor_reg_hours"]=>
string(4) "0.50"
["ticket_labor_ot_hours"]=>
string(4) "0.00"
["ticket_labor_travel_hours"]=>
string(4) "0.00"
["ticket_labor_ot_travel_hours"]=>
string(4) "0.00"
}
[1]=>
array(10) {
["ticket_labor_ot_travel_c"]=>
string(4) "0.00"
["ticket_labor_travel_c"]=>
string(4) "0.00"
["ticket_labor_ot_c"]=>
string(4) "0.00"
["ticket_labor_reg_c"]=>
string(4) "0.00"
["ticket_labor_user_id"]=>
string(1) "0"
["ticket_labor_tot_hours"]=>
string(4) "0.00"
["ticket_labor_reg_hours"]=>
string(4) "0.00"
["ticket_labor_ot_hours"]=>
string(4) "0.00"
["ticket_labor_travel_hours"]=>
string(4) "0.00"
["ticket_labor_ot_travel_hours"]=>
string(4) "0.00"
}
}
(while keeping in mind, that much like the previous question, the above array might have 70 arrays inside of it)
I get back something like:
array(10) {
["ticket_labor_ot_travel_c"]=>
string(5) "0.00"
["ticket_labor_travel_c"]=>
string(5) "0.00"
["ticket_labor_ot_c"]=>
string(5) "0.00"
["ticket_labor_reg_c"]=>
string(5) "0.00"
["ticket_labor_user_id"]=>
string(5) "0.00"
["ticket_labor_tot_hours"]=>
string(4) "0.00"
["ticket_labor_reg_hours"]=>
string(4) "0.00"
["ticket_labor_ot_hours"]=>
string(4) "0.00"
["ticket_labor_travel_hours"]=>
string(4) "0.00"
["ticket_labor_ot_travel_hours"]=>
string(4) "0.00"
}
One array, with all the arrays of the previous array of arrays compressed and their $key=>$value
's added together.
Whats the issue? regardelss of where I var_dump
or echo
, be it the $key
, $value
or even the $innerArray
I end up getting tons of notices saying:
Notice: Undefined index: ticket_labor_ot_travel_hours in C:\xampp\htdocs\rms\site\web\module\Report\controller\Index.controller.php on line 146
each notice is different for each key in the $innerArray
. So I thought lets do if(isset($key) && isset($value)){ ... }
Nope, same issue.
I have checked the outer array and can gaurentee that whats coming in is what I want, all the keys are set.
If your wondering what line 146 is: $result[$key] = number_format($result[$key] + $value, 2);
Any help?