So as of late I have been asking a lot of questions regarding adding the values of all arrays in an array of arrays into one array. That's been fine and dandy - sort of.
I am faced with an issue where I have something, var_dump
ed that looks like:
array(5) {
["AB"]=>
array(3) {
["Unknown"]=>
array(810) {
[0]=>
array(7) {
["REG"]=>
float(0)
["COST"]=>
float(0)
["TOT"]=>
float(0)
["OT"]=>
float(0)
["TRVL"]=>
float(0)
["TRV OT"]=>
float(0)
["TOTAL"]=>
float(0)
},
.... (809 More)
}
["SomeName"]=>
array(810) {
[0]=>
array(7) {
["REG"]=>
float(0)
["COST"]=>
float(0)
["TOT"]=>
float(0)
["OT"]=>
float(0)
["TRVL"]=>
float(0)
["TRV OT"]=>
float(0)
["TOTAL"]=>
float(0)
},
.... (809 More)
}
}
["MB"]=>
array(3) {
["SomeName"]=>
array(110) {
[0]=>
array(7) {
["REG"]=>
float(0)
["COST"]=>
float(0)
["TOT"]=>
float(0)
["OT"]=>
float(0)
["TRVL"]=>
float(0)
["TRV OT"]=>
float(0)
["TOTAL"]=>
float(0)
},
.... (109 More)
}
}
}
Note: I tried to format the array based on the data that came back, this is all real data minus the actual 800 some tickets - theirs also other provinces and more users.
As you can see in each province is a user, that user can appear in more then one province, with varying degrees of "ticket" based information. For example in Alberta (AB), SomeName has 810 tickets where as in MB that same user has 110 tickets.
So I wanted to add all these "arrays" under the user into one array, so instead of Unknown having 810 arrays they have 1 with all the values added up, keeping the same key structure.
So I wrote this:
// $array contains the sample array above
foreach($array as $province=>$user){
foreach($user as $userName=>$tickets){
$result = array();
foreach ($tickets as $ticket) {
foreach ($ticket as $key=>$value) {
$result[$key] = number_format($result[$key] + $value, 2);
$array[$province][$userName][] = $result;
}
}
}
}
But unless I am doing something wrong - I come back with 910 tickets for Unknown
instead of one array.
Ultimately Unknown
should look like:
["Unknown"]=>
array(1) {
[0]=>
array(7) {
["REG"]=>
float(0)
["COST"]=>
float(0)
["TOT"]=>
float(0)
["OT"]=>
float(0)
["TRVL"]=>
float(0)
["TRV OT"]=>
float(0)
["TOTAL"]=>
float(0)
}
}
...
Can some one tell me what I am doing wrong?