Jump to content

Merge 2 arrays based on shared value


John_A

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.