I feel quite stupid for asking this as I feel I should have been able to figure this out, but alas I am spending too much time on it and getting frustrated, so I figured I would request some help. I have a PHP array that looks as follows. This is just an example as this project is a work in progress:
$donations = array(
"2018 December" => array('30','20','100'),
"2018 November" => array('10','5','200','1000')
);
I have made several attempts at nested for and foreach loops to try to get the values out, and I can get close, but I always seem to run into different road blocks. For example, I used to have the year and months as part of the sub array. I was able to successfully pull all the values out using a for and then foreach loop, but I could not figure out how to exclude the first two elements (year and month) in the foreach. Here is the ultimate goal...
Each sub array contains individual member donations in US dollars. The array may grow during the current month as more donations come in, but once the month rolls over, the next month sub array starts populating with donations and the previous month is "archived". I need to sum all the donations for each sub array and make each value available to be represented as a percentage (round to nearest integer) as part of a bar graph that displays on the webpage.
Can anyone please help me to formalize my process?
This is what I tried initially. This is using the older uglier array. It should be noted, this version currently does not round to nearest integer. Have not figured that part out yet either. These numbers just workout nicely in this example. Also, in case you are curious, $total is calculated separately in a different part of the page. $total = the operating costs per month for the group, but in this example I have manually set it to $100.
$donate = array(
array("5","10"),
array("20","30"),
array("0"),
array("0"),
array("0")
);
$keydonate = array_keys($donate);
$taco = array();
for($i = 0; $i < count($donate); $i++) {
foreach($donate[$keydonate[$i]] as $value) {
$sum = $sum + $value;
$taco[$i] = ($sum/$total)*100;
}
}
This kinda works, but again, my logic is severely flawed here and this method has some pretty serious bugs. For example, the arrays keep adding to each other from previous months. Every month they need to reset back to default. Notice how in the screen shot, December is actually a sum of December and November and then on and on? Also, I like being able to add the month and year to the array as well, which this version is not capable of handling. I need some serious guidance and examples on the proper way to do this please. I know just enough PHP to be dangerous!
Thank you!



$sum = 0;beforeforeach($donate[$keydonateto reset the sum before the next month calculations start