PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
PHPycho
Forum Contributor
Posts: 336 Joined: Fri Jan 06, 2006 12:37 pm
Post
by PHPycho » Wed Jan 13, 2010 11:48 pm
Problem: see the inline comments in the following code:
Code: Select all
<?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
vincentfon
Forum Newbie
Posts: 3 Joined: Thu Jan 07, 2010 9:40 pm
Post
by vincentfon » Thu Jan 14, 2010 3:40 am
Code: Select all
<?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(
'key1' => 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
// )
// );
foreach($array1 as $array1key => $array1value) {
$final_array[$array1key] = array_merge($array1value, $array2[$array1key]);
}
print_r($final_array);
/*Which Results:
Array (
[key1] => Array ( [name] => Product2.1 [price] => 50 [qty_in_x] => 100 [qty_in_y] => 150 )
[key2] => Array ( [name] => Product2.2 [price] => 80 [qty_in_x] => 150 [qty_in_y] => 180 )
)
*/
is this what you want?
PHPycho
Forum Contributor
Posts: 336 Joined: Fri Jan 06, 2006 12:37 pm
Post
by PHPycho » Thu Jan 14, 2010 4:08 am
yes exactly..
many thanks
PHPycho
Forum Contributor
Posts: 336 Joined: Fri Jan 06, 2006 12:37 pm
Post
by PHPycho » Thu Jan 14, 2010 5:03 am
Code: Select all
foreach($array1 as $array1key => $array1value) {
$final_array[$array1key] = array_merge($array1value, $array2[$array1key]);
}
should be
Code: Select all
foreach($array1 as $array1key => $array1value) {
$final_array[$array1key] = array_merge($array2[$array1key], $array1value);
}
note the changes made.
Thanks