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. Link to comment https://forums.phpfreaks.com/topic/86879-combine-array/ 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? Link to comment https://forums.phpfreaks.com/topic/86879-combine-array/#findComment-444154 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/86879-combine-array/#findComment-444184 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! Link to comment https://forums.phpfreaks.com/topic/86879-combine-array/#findComment-444274 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>'; Link to comment https://forums.phpfreaks.com/topic/86879-combine-array/#findComment-444322 Share on other sites More sharing options...
everisk Posted January 21, 2008 Author Share Posted January 21, 2008 Thank you very much! Link to comment https://forums.phpfreaks.com/topic/86879-combine-array/#findComment-444769 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.