|
PHP adding arrays / summing arrays
A question from my mailbox, answered in relation to our hotel. "I have a number of arrays in PHP and I want to add the elements of the arrays together". "There doesn't appear to be an array_sum function".
So in other words, my correspondent may have room occupancy data such as:
$bed1 = array(2,2,2,0,2,0,0);
$bed2 = array(1,1,1,1,1,0,0);
$bed3 = array(1,1,1,0,0,2,2);
$bed4 = array(0,0,0,0,0,0,0);
$bed5 = array(1,1,1,1,1,2,2);
and want to produce a report showing the nightly occupancy - perhaps to know how many breakfasts to prepare. What about
for ($k=0; $k<7; $k++) {
$occupancy[$k] = $bed1[$k]+$bed2[$k]+$bed3[$k]
+$bed4[$k]+$bed5[$k];
}
That code is horrid - it works with our 5 room place, but when we grow to 50 rooms ....
A good solution is to keep the data in an array of arrays - the "1", "2", "3" and so on in the variable names really should itself be a variable / subscript so that the summing can be done with a loop of loops:
$full = array($bed1,$bed2,$bed3,$bed4,$bed5);
combines the bedrooms and the the code to sum looks like:
for ($k=0; $k<7; $k++) {
$sm = 0;
for ($j=0; $j<count($full); $j++) {
$sm += $full[$j][$k];
}
$ot[$k] = $sm;
}
Which is slightly longer for my 5 rooms, but much MUCH shorter once we expand!
(See full source code here and run it here) (written 2007-03-23, updated 2009-05-04)
Associated topics are indexed under H106 - PHP - Arrays [4072] Splitting the difference with PHP - (2013-04-27) [4068] Arrays in PHP - contain different and even mixed data types - (2013-04-24) [3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01) [3379] Sorting data the way YOU want it sorted - (2011-08-05) [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18) [2920] Sorting - naturally, or into a different order - (2010-08-14) [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11) [2274] PHP preg functions - examples and comparision - (2009-07-08) [2215] If nothing, make it nothing. - (2009-06-02) [1614] When an array is not an array - (2008-04-17) [1451] More PHP sample and demonstration programs - (2007-12-01) [1199] Testing for one of a list of values. - (2007-05-22) [832] Displaying data at 5 items per line on a web page - (2006-08-14) [773] Breaking bread - (2006-06-22) [603] PHP - setting sort order with an associative array - (2006-02-13) [409] Functions and commands with dangerous names - (2005-08-11)
54a2
Some other Articles
1200 miles in 3 daysThe Last Word on the roadOrigin of Jack and Jill and little Jack HornerNewport Male Voice ChoirPHP adding arrays / summing arraysUnexpected visitors to our sitePHP Image upload scriptFile and URL reading in PHPBank Holiday country breaks in Melksham, WiltshireTraining in Lua
|
4084 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|