i have array1 with following content
array(
'banana',
'apple',
'orange');
and i have a different associative array2 with content and prices of items
array('banana' => '1.45',
'apple' => '2.99',
'carrot' => '1.99',
'orange' => '0.99',
'papaya' => '2.99');
how do i generate a final array, that combines the two, with their common parts, that i get this final result:
array('banana' => '1.45',
'apple' => '2.99',
'orange' => '0.99');
You can do this:
$fruitNames = array(
'banana',
'apple',
'orange');
$fruitValues = array('banana' => '1.45',
'apple' => '2.99',
'orange' => '0.99');
$finalArray = array();
foreach($fruitNames as $value)
{
$finalArray[$value] = $fruitValues[$value];
}
$finalArray
will have the expected value.
I would use array_intersect_key()
here.
$intersection = array_intersect_key($second, array_flip($first));
use array_key_exists
to loop thru and make new array