matthewtbaker Posted October 26, 2012 Share Posted October 26, 2012 Hi phpfreak-ers, $BigArray[] = array('url' => 'www.example.com', 'referer' => 'refer1'); $BigArray[] = array('url' => 'www.demo.com', 'referer' => 'refer1'); $SmallArray[] = array('url' => 'www.example.com', 'referer' => 'refer2'); Basically I want to compare the two example arrays (above) and if a duplicate 'url' is found then I'd like to merge the 'referer' values in $BigArray otherwise add as an additional record. So I should be left with: Not syntax just used to explain my objective $BigArray url='www.example.com', 'referer' = 'refer1refer2'. $BigArray url='www.demo.com', 'referer' = 'refer1'. I've tried a day and a half worth of approaches and can't get it working. Thanks for your help in advance! Matt Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/ Share on other sites More sharing options...
PravinS Posted October 26, 2012 Share Posted October 26, 2012 Try using PHP "array_intersect()" function. Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/#findComment-1387868 Share on other sites More sharing options...
matthewtbaker Posted October 26, 2012 Author Share Posted October 26, 2012 On 10/26/2012 at 10:31 AM, pbs said: Try using PHP "array_intersect()" function. Hi pbs, From what I can work out the array_intersect() function does not seem to support multidimensional arrays. It also does not merge values depending on whether a specified key matches or not. Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/#findComment-1387869 Share on other sites More sharing options...
Muddy_Funster Posted October 26, 2012 Share Posted October 26, 2012 Off the top of my head something like this should be close: for($i=0;$i<=count($smallArray)-1;$i++){ foreach($bigArray as $sdArray){ if($smallArray[$i]['url'] == $sdArray['url']){ $sdArray['refferer'] = $sdArray['refferer'].$smallArray[$i]['refferer']; } } } Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/#findComment-1387898 Share on other sites More sharing options...
matthewtbaker Posted October 26, 2012 Author Share Posted October 26, 2012 On 10/26/2012 at 12:35 PM, Muddy_Funster said: Off the top of my head something like this should be close: for($i=0;$i<=count($smallArray)-1;$i++){ foreach($bigArray as $sdArray){ if($smallArray[$i]['url'] == $sdArray['url']){ $sdArray['refferer'] = $sdArray['refferer'].$smallArray[$i]['refferer']; } } } Thanks Muddy_Funster but unfortunately this does not work. Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/#findComment-1387930 Share on other sites More sharing options...
Muddy_Funster Posted October 26, 2012 Share Posted October 26, 2012 well I didn't expect t it to work out the box, after all, i did say close can you give any advance one "this does not work" ? Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/#findComment-1387937 Share on other sites More sharing options...
matthewtbaker Posted October 29, 2012 Author Share Posted October 29, 2012 Basing my approach on Muddy_Funster I came up with the below which successfully resolves the original question. I thought I would share it in case anyone comes across this post in the future. for ($iBigArrayIndex = 0; $iBigArrayIndex < sizeof($iBigArrayIndex); $iBigArrayIndex++) { for ($iSmallArrayIndex = 0; $iSmallArrayIndex < sizeof($SmallArray); $iSmallArrayIndex++) { if ($SmallArray[$iSmallArrayIndex]['url'] == $BigArray[$iBigArrayIndex]['url']) { $BigArray[$iBigArrayIndex]['referrer'] = $BigArray[$iBigArrayIndex]['referrer'] . ',' . $SmallArray[$iSmallArrayIndex]['referrer']; } else { $BigArray[] = array('url' => $SmallArray[$iSmallArrayIndex]['url'], 'referrer' => $SmallArray[$iSmallArrayIndex]['referrer']); } } } I was surprised that there was not a standard PHP function that does this. Does anyone have a neater/cleaner way of doing this? Matt Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/#findComment-1388398 Share on other sites More sharing options...
matthewtbaker Posted October 31, 2012 Author Share Posted October 31, 2012 Just for the record; I simplied the code using a foreach loop. foreach ($BigArray as $key => $aBigArrayValue) { foreach ($aSmallArray as $sSmallValue) { } } Link to comment https://forums.phpfreaks.com/topic/269930-compare-and-merge-multidimensional-array-values/#findComment-1388982 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.