/    Sign up×
Community /Pin to ProfileBookmark

array issues with intersect

Problem: see the inline comments in the following code:

[code=php]<?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’
*/
?>[/code]

Is there any way to accomplish as mentioned above?
Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@GenixdeaeJan 14.2010 — Looks like [url=http://www.php.net/manual/en/function.array-diff-key.php]array_diff_key()[/URL] possibly? I'm not very well versed with arrays yet, but maybe will get you started.
×

Success!

Help @PHPycho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...