everisk Posted January 20, 2008 Share Posted January 20, 2008 I have an array that looks like Array ( [1] => Array ( [url] => http://www.abcde.com [stimes] => 823 [pp] => 26.97 ) [2] => Array ( [url] => http://www.12345.com [stimes] => 561 [pp] => 18.38 ) [3] => Array ( [url] => http://www.xyz123.com [stimes] => 1 [pp] => 0.03 ) [4] => Array ( [url] => http://www.xyz123.com [stimes] => 1 [pp] => 0.03 ) [5] => Array ( [url] => http://www.xyz123.com [stimes] => 1 [pp] => 0.03 ) ) How do I combine those with the same URL together? The [stimes] will also need to be accumulated. Many thanks in advance. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted January 20, 2008 Share Posted January 20, 2008 what do you mean combine them together? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 20, 2008 Share Posted January 20, 2008 try <?php $data = Array ( 1 => Array ( 'url' => 'http://www.abcde.com', 'stimes' => 823, 'pp' => 26.97 ), 2 => Array ( 'url' => 'http://www.12345.com', 'stimes' => 561, 'pp' => 18.38 ), 3 => Array ( 'url' => 'http://www.xyz123.com', 'stimes' => 1, 'pp' => 0.03 ), 4 => Array ( 'url' => 'http://www.xyz123.com', 'stimes' => 1, 'pp' => 0.03 ), 5 => Array ( 'url' => 'http://www.xyz123.com', 'stimes' => 1, 'pp' => 0.03 ) ); $combined = array(); foreach ($data as $item) { if (isset($combined[$item['url']]['stimes'])) { $combined[$item['url']]['stimes'] += $item['stimes']; } else { $combined[$item['url']]['stimes'] = $item['stimes']; } $combined[$item['url']]['pp'] = $item['pp']; } /** * view results */ echo '<pre>', print_r($combined, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
everisk Posted January 20, 2008 Author Share Posted January 20, 2008 With the code, I now got array like Array ( [http://www.abcde.com] => Array ( [stimes] => 823 [pp] => 26.97 ) [http://www.12345.com] => Array ( [stimes] => 561 [pp] => 18.38 ) [http://www.xyz123.com] => Array ( [stimes] => 3 [pp] => 0.03 ) ) What do I need to change to get array like. I need to traverse array but number. Array ( 1 => Array ( 'url' => 'http://www.abcde.com', 'stimes' => 823, 'pp' => 26.97 ), 2 => Array ( 'url' => 'http://www.12345.com', 'stimes' => 561, 'pp' => 18.38 ), 3 => Array ( 'url' => 'http://www.xyz123.com', 'stimes' => 3, 'pp' => 0.03 ), ); Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Barand Posted January 20, 2008 Share Posted January 20, 2008 add <?php $i=0; $combined2 = array(); foreach ($combined as $k => $a) $combined2[++$i] = array ('url' => $k, 'stimes' => $a['stimes'], 'pp' => $a['pp']); /** * view results */ echo '<pre>', print_r($combined2, true), '</pre>'; Quote Link to comment Share on other sites More sharing options...
everisk Posted January 21, 2008 Author Share Posted January 21, 2008 Thank you very much! Quote Link to comment 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.