John_A Posted March 27, 2012 Share Posted March 27, 2012 I have one array, called for example $MainImages : - Array ( [0] => Array ( [widgetID] => 143 [image] => image_a.jpg ) [1] => Array ( [widgetID] => 147 [image] => image_b.jpg ) [2] => Array ( [widgetID] => 73 [image] => image_c.jpg ) ) and a second array, we'll call it $widgets: - Array ( [0] => Array ( [widgetID] => 147 [seo_title] => An excellent class B Widget [name] => Widget B [categoryID] => 1 ) [1] => Array ( [widgetID] => 73 [seo_title] => An excellent class C Widget [name] => Widget C [categoryID] => 1 ) [2] => Array ( [widgetID] => 143 [seo_title] => An excellent class A Widget [name] => Widget A [categoryID] => 1 ) ) I want to merge the value from the [][image] key from the first array into a new [][image] key / value in the second, using a shared value as a reference...so the desired result array is: - Array ( [0] => Array ( [widgetID] => 147 [seo_title] => An excellent class B Widget [name] => Widget B [categoryID] => 1 [image] => image_b.jpg ) [1] => Array ( [widgetID] => 73 [seo_title] => An excellent class C Widget [name] => Widget C [categoryID] => 1 [image] => image_c.jpg ) [2] => Array ( [widgetID] => 143 [seo_title] => An excellent class A Widget [name] => Widget A [categoryID] => 1 [image] => image_a.jpg ) ) Obviously the order is different and so they need to be merged based on the widgetID values. Also, the same widgetID may be in each array more than once. I've looked at a lot of help but can't find anything to do exactly what I need....any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/259825-merge-2-arrays-based-on-shared-value/ Share on other sites More sharing options...
VTS Posted March 27, 2012 Share Posted March 27, 2012 You could try something like the following: foreach($ar1 as $img) { for($i = 0; $i < count($ar2); $i++) { if($ar2[$i]['widgetID'] == $img['widgetID']) { $ar2[$i]['image'] = $img['image']; } else { // the widgetID is not found... do what you need with it here } } } Where $ar1 is the array containing the images and $ar2 is the one containing your other data. Not very efficient since it loops through the second array every time but this is just what came to the top of my head quickest. Someone else might want to post up a more efficient suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/259825-merge-2-arrays-based-on-shared-value/#findComment-1331642 Share on other sites More sharing options...
Jessica Posted March 27, 2012 Share Posted March 27, 2012 Is it possible for you to restructure your arrays, when you select them from their source (I assume a DB) so the array key IS the id? Also if you're selecting these from a DB, you could save a lot of work by using a JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/259825-merge-2-arrays-based-on-shared-value/#findComment-1331643 Share on other sites More sharing options...
John_A Posted March 27, 2012 Author Share Posted March 27, 2012 You could try something like the following: foreach($ar1 as $img) { for($i = 0; $i < count($ar2); $i++) { if($ar2[$i]['widgetID'] == $img['widgetID']) { $ar2[$i]['image'] = $img['image']; } else { // the widgetID is not found... do what you need with it here } } } Where $ar1 is the array containing the images and $ar2 is the one containing your other data. Not very efficient since it loops through the second array every time but this is just what came to the top of my head quickest. Someone else might want to post up a more efficient suggestion. Thanks, I'll give that a try and stick with it unless anyone has a more efficient method! Is it possible for you to restructure your arrays, when you select them from their source (I assume a DB) so the array key IS the id? Also if you're selecting these from a DB, you could save a lot of work by using a JOIN. The source is a DB from a 3rd party application, which I don't want to interfere with. I actually edited it with a JOIN but had to keep applying the edit every upgrade, this new approach is as a 'plug-in' to basically inject what I need into the existing array. Quote Link to comment https://forums.phpfreaks.com/topic/259825-merge-2-arrays-based-on-shared-value/#findComment-1331674 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.