matthewtbaker Posted October 26, 2012 Share Posted October 26, 2012 (edited) 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 Edited October 26, 2012 by matthewtbaker Quote 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. Quote 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 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. Quote 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 (edited) 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']; } } } Edited October 26, 2012 by Muddy_Funster Quote 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 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. Quote 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" ? Quote 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 Quote 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) { } } Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.