majma Posted November 12, 2008 Share Posted November 12, 2008 I have such array: ( [0] => Array ( [ownerid] => 100073 [shipping] => 10 ) [1] => Array ( [ownerid] => 107266 [shipping] => 20 ) [2] => Array ( [ownerid] => 107266 [shipping] => 35 ) [3] => Array ( [ownerid] => 100073 [shipping] => 40 ) [4] => Array ( [ownerid] => 123456 [shipping] => 80 ) ) I'd like to sum 'shipping' for each 'ownerid', in short the result should look like that: ( [0] => Array ( [ownerid] => 100073 [shipping] => 50 ) [1] => Array ( [ownerid] => 107266 [shipping] => 55 ) [2] => Array ( [ownerid] => 123456 [shipping] => 80 ) ) Can anyone give me a hint on that? Link to comment https://forums.phpfreaks.com/topic/132403-how-to-sum-in-multidimesional-array/ Share on other sites More sharing options...
Mark Baker Posted November 12, 2008 Share Posted November 12, 2008 <?php $testData = array ( array ( 'ownerid' => 100073, 'shipping' => 10 ), array ( 'ownerid' => 107266, 'shipping' => 20 ), array ( 'ownerid' => 107266, 'shipping' => 35 ), array ( 'ownerid' => 100073, 'shipping' => 40 ), array ( 'ownerid' => 123456, 'shipping' => 80 ) ); $testResult = array(); foreach ($testData as $testDatum) { $ownerID = $testDatum['ownerid']; $shipping = $testDatum['shipping']; $found = False; foreach ($testResult as &$resultDatum) { if ($ownerID == $resultDatum['ownerid']) { $found = True; $resultDatum['shipping'] += $shipping; } } if (!$found) { $testResult[] = array( 'ownerid' => $ownerID, 'shipping' => $shipping ); } } echo '<pre>'; print_r($testResult); echo '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/132403-how-to-sum-in-multidimesional-array/#findComment-688376 Share on other sites More sharing options...
dymon Posted November 12, 2008 Share Posted November 12, 2008 <?php //Your initial array $shops = array ( 0 => array ( 'ownerid' => 100073, 'shipping' => 10 ), 1 => array ( 'ownerid' => 107266, 'shipping' => 20 ), 2 => array ( 'ownerid' => 107266, 'shipping' => 35 ), 3 => array ( 'ownerid' => 100073, 'shipping' => 40 ), 4 => array ( 'ownerid' => 123456, 'shipping' => 80 ) ); //Create an array that will keep the ownerid as key and the sum of shippings as value $inter_shops = array (); foreach ($shops as $data) { $inter_shops [$data['ownerid']] += $data['shipping']; } //You can check if this way is enough print_r ($inter_shops); //Create the array anyway you want (the way you want it in this case) $final_shops = array (); $count = 0; foreach ($inter_shops as $ownerid => $shipping) { $final_shops[$count]['ownerid'] = $ownerid; $final_shops[$count]['shipping'] = $shipping; $count++; } //See the result print_r ($final_shops); ?> Link to comment https://forums.phpfreaks.com/topic/132403-how-to-sum-in-multidimesional-array/#findComment-688383 Share on other sites More sharing options...
majma Posted November 12, 2008 Author Share Posted November 12, 2008 Thank you very much, you are a genious! I've been working on it most of the day and nothing. Link to comment https://forums.phpfreaks.com/topic/132403-how-to-sum-in-multidimesional-array/#findComment-688395 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.