I'm trying to learn about arrays and how they work. The problem is that I have an include file in the following format that I need to add the total days that each piece of equipment is rented for and display it in a report.
<?php
$rentals =
"Equipment01 1 3 13
Equipment99 11 17 22"
?>
Each row contains a set of 4 values. The equipment serial#, the number equivalent of the month, the day of the month and the quantity of days the equipment was rented. Currently I have a long if/elseif statement and count/accumulator variables to do the task and it works fine. However, I'm trying to learn more efficient ways of coding and from what I've been reading, I think an array is the best option.
This is just part of the task as the include file does not contain the daily rental rate for each piece of equipment. If I understand it well enough, my thought is that one array would be generated from the include file and another array would be hard coded with the daily rates. After these steps, a report would need to be generated to show each piece of equipment and totals for how many days each were rental and the total amount of revenue. Sorting the data by equipment with the most revenue would be a cool feature.
This is a class assignment that I already completed and it works as it needs to, but I'm trying to take it further and learn more.
For additional info on the process, you can find details at...
viewtopic.php?f=50&t=134320&p=670987#p670987
I might be biting off more than I can handle, but learning is a process.
An array to add and calculate an include file.
Moderator: General Moderators
Re: An array to add and calculate an include file.
You could have a $totals array with the equipment serial number as the index and an array containing days rented and revenue as the value.
or something to that effect. As you traverse $rentals, you can use array_key_exists to see if there is already an entry for that particular piece of equipment and either add a new key or append the current values as needed.
Code: Select all
$totals = array('equipment01' => array('days' => 3, 'revenue' => 10));Re: An array to add and calculate an include file.
Thanks Celauran as this gives me something to work with. I'm still unsure how this might work, but I have the PHP manual open to study array_key_exists. I don't mind trying to figure thinks out by myself, but sometimes I need a little push in the right direction.Celauran wrote:You could have a $totals array with the equipment serial number as the index and an array containing days rented and revenue as the value.or something to that effect. As you traverse $rentals, you can use array_key_exists to see if there is already an entry for that particular piece of equipment and either add a new key or append the current values as needed.Code: Select all
$totals = array('equipment01' => array('days' => 3, 'revenue' => 10));
BTW... I will have to read through some of your other posts Celauran as your code seems to be short and to the point from the little that I'm seen so far.