Juan1989 Posted October 31, 2014 Share Posted October 31, 2014 I have an array that has several amounts (based on $$$ sales) attached to a 'name' 'id' and 'goal'. As you can see some of the names, id's, and goals are the same. My goal is to gather a total of amounts and attach each total to whichever 'name', 'id', and 'goal' that made the sale. I'm honestly not sure how to go about this as I'm still learning. Array ( [0] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 360.00 ) [1] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 450.00 ) [2] => Array ( [name] => L.Chane [id] => oper-4bceffd1-21e0af5b [goal] => 2014-10-25000 [amount] => 450.00 ) [3] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 370.00 ) [19] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 175.00 ) [20] => Array ( [name] => C.James [id] => oper-4c236420-0b11e945 [goal] => 2014-10-25000 [amount] => 155.00 )[61] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 200.00 ) [62] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 599.00 ) [63] => Array ( [name] => K.Crass [id] => oper-4c597644-402490ee [goal] => 2014-10-25000 [amount] => 50.00 ) [113] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 450.00 ) [114] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 589.00 ) [115] => Array ( [name] => R.Cervantes [id] => oper-4f05a90b-03b379f9 [goal] => 2014-10-25000 [amount] => 350.00 ) [166] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 375.00 ) [167] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 294.50 ) [168] => Array ( [name] => A.Gerred [id] => oper-4f30019a-27f27473 [goal] => 2014-10-25000 [amount] => 440.00 ) [202] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 5.00 ) [203] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 310.00 ) [204] => Array ( [name] => G.Whitcher [id] => oper-4f300d33-de9592e3 [goal] => 2014-10-25000 [amount] => 349.00 ) [235] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 499.00 ) [236] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 187.50 ) [237] => Array ( [name] => K.Lawrence [id] => oper-50f6e4ad-9effbec7 [goal] => 2014-10-25000 [amount] => 170.00 ) [246] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 375.00 ) [247] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 187.50 ) [248] => Array ( [name] => K.Chane [id] => oper-52657816-3d6516e2 [goal] => 2014-10-25000 [amount] => 229.50 ) [256] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 170.00 ) [257] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 584.00 ) [258] => Array ( [name] => J.Stewart [id] => oper-qtgjvw8y-1uqtw058 [goal] => 2014-10-25000 [amount] => 249.50 ) ) Here is my code so far (And I know it's not clean, because I'm not using PDO yet, I'm just trying to get it to work): $result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID WHERE a.ChargeAmount IS NOT NULL AND a.PaymentStatus='OK' AND a.PaymentDate LIKE '$currentDate%' AND u.MonthlyGoal LIKE '$currentDate%' " ); while ($row = mysql_fetch_assoc($result)) { $operArray[] = array( 'name' => $row['OperatorName'], 'id' => $row['OperatorID'], 'goal' => $row['MonthlyGoal'], 'amount' => $row['ChargeAmount']); } foreach ($operArray as $value) { if($value['id'] == '' || $value['id'] == null) { continue; } if(array_key_exists($value['id'], $operSums)) { $operSums[$value['id']] += $value['amount']; } else { $operSums[$value['id']] = $value['amount']; } } Any help is greatly appreciated. Associative arrays are just not my cup of tea. Quote Link to comment https://forums.phpfreaks.com/topic/292198-associative-arrays/ Share on other sites More sharing options...
Barand Posted October 31, 2014 Share Posted October 31, 2014 Why don't you get the total when you query the table? SELECT a.OperatorID , u.OperatorName , u.MonthlyGoal , SUM(a.ChargeAmount) as total FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID AND u.MonthlyGoal LIKE '$currentDate%' WHERE a.ChargeAmount IS NOT NULL AND a.PaymentStatus='OK' AND a.PaymentDate LIKE '$currentDate%' GROUP BY a.OperatorID " ); Quote Link to comment https://forums.phpfreaks.com/topic/292198-associative-arrays/#findComment-1495397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.