Help with array_intersect_key()?

Problem: see the inline comments in the following code:

<?php
$array1 = array(
	'key1' => array(
		'name' => 'Product1.1'
		,'price' => 220
		,'qty_in_x' => 100
	)
	,'key2' => array(
		'name' => 'Product1.2'
		,'price' => 120
		,'qty_in_x' => 150
	)
	/* and so on... */
);

$array2 = array(
	'key11' => array(
		'name' => 'Product2.1'
		,'price' => 50
		,'qty_in_y' => 150
	)
	,'key2' => array(
		'name' => 'Product2.2'
		,'price' => 80
		,'qty_in_y' => 180
	)
	/* and so on... */
);

//what i want to do is intersect the two arrays by keys and want to get the results as:
$final_array = array(
	'key2' => array(
		'name' => 'Product1.2'
		,'price' => 120
		,'qty_in_x' => 150
		,'qty_in_y' => 180 //Note: this should be merged from $array2
	)
);

//I tried with:
$final_array = array_intersect_key($array1, $array2);
print_r($final_array);
/*Which Results:
Array
(
    [key2] => Array
        (
            [name] => Product1.2
            [price] => 120
            [qty_in_x] => 150
        )

)
which just gave the fields from $array1 excluding 'qty_in_y'
*/
?>

Is there any way to accomplish as mentioned above?
Thanks

This is your clue

//Note: this should be merged

Intersect means find the common elements of two arrays, not the unique ones.

I’m suspecting there is either something wrong with your logic or example, though. Why would product 1 care what product 2 has in x or y?

I think you didn’t get my points.
Let me explain.
There are two arrays and i am intersecting on the basis of keys (irrelevant of values).
So on that basis we will get an array with common key: ‘key2’ as:

Array
(
    [key2] => Array
        (
            [name] => Product1.2
            [price] => 120
            [qty_in_x] => 150
        )

)

since the master array is $array1.
Now look at the slave array $array2 with key: ‘key2’ which is given as:

'key2' => array(
		'name' => 'Product2.2'
		,'price' => 80
		,'qty_in_y' => 180
	)

and i want to have this ‘qty_in_y’ merged in above results.
Hope my question is clear now. I guessed so.
Thanks

Still doesn’t make much logical sense, but how about this:


foreach($arr1 as $k => $v) {
    array_merge($v, array_diff_key($v, $arr2[$k]));
}

(after you have done the intersect)

I don’t want to loop after intersection.
I was wondering to know if there is such function which intersect with key and merge the new fields(like in our case).
Thanks

No.

I deployed with simpler approach(without using array_intersect_key()):

$final_array = array();
foreach($array1 as $key => $value) {
	if(isset($array2[$key])){
		$final_array[$key] = array_merge($value, $array2[$key]);
	}
}

any other ways?

That won’t do what you specified. With array_merge() the second array will overwrite the first, so instead of name => 1.2 you will have name => 2.2

But whatever … you spec’d it right, your logic is flawless, only our answers are faulty.

Sorry my mistake.
array_merge($value, $array2[$key]) >==> array_merge($array2[$key], $value)
So it becomes:

$final_array = array();
foreach($array1 as $key => $value) {
    if(isset($array2[$key])){
        $final_array[$key] = array_merge($array2[$key], $value);
    }
}