dijix316 Posted August 17, 2009 Share Posted August 17, 2009 Hopefully my posting title makes sense. I have the results from a MSSQL query in an associative array. Here is a small example of what a few rows of my results may look like: Array ([usrPK] => 22 [usrName] =>Bob [usrDate]=>08/12/09 [b][usrQty]=>2[/b]) Array ([usrPK] => 22 [usrName] =>Bob [usrDate]=>08/12/09 [b][usrQty]=>0.25[/b]) Array ([usrPK] => 10 [usrName] =>Billy [usrDate]=>08/12/09 [usrQty]=>1) What I would like to have is: Array ([usrPK] => 22 [usrName] =>Bob [usrDate]=>08/12/09 [b][usrQty]=>2.25[/b]) Array ([usrPK] => 10 [usrName] =>Billy [usrDate]=>08/12/09 [usrQty]=>1) Basically Bob has two records on this date which I would like combined into one with the sum of array([usrQty]) value from the two results. Now I'd prefer to do this post SQL query, but if I can't how would I format my MSSQL query to accomplish this? Thanks in advance for your help. Link to comment https://forums.phpfreaks.com/topic/170753-combine-array-values-with-same-indexes/ Share on other sites More sharing options...
.josh Posted August 17, 2009 Share Posted August 17, 2009 sum() Link to comment https://forums.phpfreaks.com/topic/170753-combine-array-values-with-same-indexes/#findComment-900531 Share on other sites More sharing options...
sasa Posted August 18, 2009 Share Posted August 18, 2009 <?php $test = array( Array ('usrPK' => 22, 'usrName' =>'Bob', 'usrDate'=>'08/12/09', 'usrQty'=>'2'), Array ('usrPK' => 22, 'usrName' =>'Bob', 'usrDate'=>'08/12/09', 'usrQty'=>'0.25'), Array ('usrPK' => 10, 'usrName' =>'Billy', 'usrDate'=>'08/12/09', 'usrQty'=>'1') ); $out = array(); foreach ($test as $v){ if (isset($out[$v['usrPK']])) $out[$v['usrPK']]['usrQty']+=$v['usrQty']; else $out[$v['usrPK']]=$v; } print_r($out); ?> Link to comment https://forums.phpfreaks.com/topic/170753-combine-array-values-with-same-indexes/#findComment-900629 Share on other sites More sharing options...
dijix316 Posted August 18, 2009 Author Share Posted August 18, 2009 Thanks sasa! That worked perfect. Link to comment https://forums.phpfreaks.com/topic/170753-combine-array-values-with-same-indexes/#findComment-900929 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.