[PHP] Summing array indexes.
Is there an easy way to loop thru the array below and add the incremented total price indexes so the order_total_price index contains the correct sum. Any help would be greatly appreciated. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) } Like this. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) 30.00 } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) 40.00 } Tom Shaw [EMAIL PROTECTED]
Re: [PHP] Summing array indexes.
Not a direct answer to your question (don't worry, I hate it when people do this to me too), but one thought I had was to have all of the products ordered as their own array. [0] = array(15) { [order_date] = string(8) 09-01-08 [order_products] = array(2) { [0] = string(5) 10.00 [1] = string(5) 20.00 } [order_total_price] = string(0) } In this case, it would be trivial to write a foreach loop on the 'order_products' array to calculate the total. Otherwise, run a foreach over the array, looking for keys which begin with order_product_price_ and, when found, grab the price and add it to order_total_price. On Sep 4, 2008, at 6:02 PM, Tom Shaw wrote: --=_NextPart_000_004B_01C90EAF.FA964EB0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Is there an easy way to loop thru the array below and add the incremented total price indexes so the order_total_price index contains the correct sum. Any help would be greatly appreciated. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) } Like this. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) 30.00 } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) 40.00 } Tom Shaw [EMAIL PROTECTED] --=_NextPart_000_004B_01C90EAF.FA964EB0-- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Summing array indexes.
This should help: http://us.php.net/array_walk Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Tom Shaw wrote: Is there an easy way to loop thru the array below and add the incremented total price indexes so the order_total_price index contains the correct sum. Any help would be greatly appreciated. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) } Like this. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) 30.00 } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) 40.00 } Tom Shaw [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Summing array indexes.
On Thu, Sep 4, 2008 at 11:02 PM, Tom Shaw [EMAIL PROTECTED] wrote: Is there an easy way to loop thru the array below and add the incremented total price indexes so the order_total_price index contains the correct sum. Any help would be greatly appreciated. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) } Like this. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) 30.00 } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) 40.00 } Tom Shaw [EMAIL PROTECTED] foreach( $arr_top as $arr_top_elem ) { $total = 0; foreach( $arr_top_elem as $key = $elem ) { if ( strpos( 'order_product_price_' ) === 0 ) { $total += $elem; } } $arr_top_elem['order_total_price'] = $total; } This should do da trick... -- Thanks, Diogo Neves Web Developer @ SAPO.pt by PrimeIT.pt
RE: [PHP] Summing array indexes.
Thanks for every ones help. I just used a bunch of control structures and issest's to add it up. Not pretty but works. if (isset($data['order_product_amount_0'])) { $data['order_product_total'] = number_format($data['order_product_amount_0'], 2, '.', ''); } if (isset($data['order_product_amount_1'])) { $data['order_product_total'] = number_format($data['order_product_total'] + $data['order_product_amount_1'], 2, '.', ''); } -Original Message- From: Eric Gorr [mailto:[EMAIL PROTECTED] Sent: Thursday, September 04, 2008 5:17 PM To: PHP General; Tom Shaw Subject: Re: [PHP] Summing array indexes. Not a direct answer to your question (don't worry, I hate it when people do this to me too), but one thought I had was to have all of the products ordered as their own array. [0] = array(15) { [order_date] = string(8) 09-01-08 [order_products] = array(2) { [0] = string(5) 10.00 [1] = string(5) 20.00 } [order_total_price] = string(0) } In this case, it would be trivial to write a foreach loop on the 'order_products' array to calculate the total. Otherwise, run a foreach over the array, looking for keys which begin with order_product_price_ and, when found, grab the price and add it to order_total_price. On Sep 4, 2008, at 6:02 PM, Tom Shaw wrote: --=_NextPart_000_004B_01C90EAF.FA964EB0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Is there an easy way to loop thru the array below and add the incremented total price indexes so the order_total_price index contains the correct sum. Any help would be greatly appreciated. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) } Like this. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) 30.00 } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) 40.00 } Tom Shaw [EMAIL PROTECTED] --=_NextPart_000_004B_01C90EAF.FA964EB0-- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Summing array indexes.
Tom Shaw schreef: Thanks for every ones help. I just used a bunch of control structures and issest's to add it up. Not pretty but works. if (isset($data['order_product_amount_0'])) { $data['order_product_total'] = number_format($data['order_product_amount_0'], 2, '.', ''); } if (isset($data['order_product_amount_1'])) { $data['order_product_total'] = number_format($data['order_product_total'] + $data['order_product_amount_1'], 2, '.', ''); } foreach ($orders as $data) { $total = 0; foreach ($data as $key = $val) { if (strpos('order_product_amount', $key) === 0) $total += (float)$val; } $data['order_product_total'] = number_format($total, 2, '.', ''); } -Original Message- From: Eric Gorr [mailto:[EMAIL PROTECTED] Sent: Thursday, September 04, 2008 5:17 PM To: PHP General; Tom Shaw Subject: Re: [PHP] Summing array indexes. Not a direct answer to your question (don't worry, I hate it when people do this to me too), but one thought I had was to have all of the products ordered as their own array. [0] = array(15) { [order_date] = string(8) 09-01-08 [order_products] = array(2) { [0] = string(5) 10.00 [1] = string(5) 20.00 } [order_total_price] = string(0) } In this case, it would be trivial to write a foreach loop on the 'order_products' array to calculate the total. Otherwise, run a foreach over the array, looking for keys which begin with order_product_price_ and, when found, grab the price and add it to order_total_price. On Sep 4, 2008, at 6:02 PM, Tom Shaw wrote: --=_NextPart_000_004B_01C90EAF.FA964EB0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Is there an easy way to loop thru the array below and add the incremented total price indexes so the order_total_price index contains the correct sum. Any help would be greatly appreciated. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) } Like this. array(5) { [0] = array(15) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 10.00 [order_product_price_2] = string(5) 20.00 [order_total_price] = string(0) 30.00 } [1] = array(19) { [order_date] = string(8) 09-01-08 [order_product_price_1] = string(5) 25.00 [order_product_price_2] = string(5) 15.00 [order_total_price] = string(0) 40.00 } Tom Shaw [EMAIL PROTECTED] --=_NextPart_000_004B_01C90EAF.FA964EB0-- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php