In my coding there are two while loops which will produce customer-id and their amount
PHP Code:
$array []= $customerid;
$array []= $amount;
// following is outside of two while loops
showresult($array); // calling a function
function showresult($array) // code given by you both
{
print_r($array) // this will give $array = Array ( 0 => 2644, 1 => 10, 2 => 2644, 3 => 50, 4 => 2644, 5 => 10, 6 => 5172, 7 => 70, 8 => 6546, 9 => 100 );
// the above array will produce incorrect results
$array = Array( 2644,10,2644,50,2644,10,5172,70,6546,100 ); // array taken by you
// the above array will produce correct results
// So i want my array to be taken as Array( 2644,10,2644,50,2644,10,5172,70,6546,100 );
foreach( $array as $key => $value ){
// use the % (modulus (remainder after division)) to determine if the key is odd or even
if( $key%2 === 0 ){
// it's even (or 0) --a customer number
$cust_no[$value] = isset($cust_no[$value])? $cust_no[$value] + $array[$key + 1] : $array[$key + 1];
}
}
// view results:
foreach( $cust_no as $k => $v ){
print "customer #$k has amount $v<br>";
$assoc[] = $v;
}
print "Sum of all amounts: " . array_sum( $assoc );
}
Bookmarks