How can I get the key intersection of two arrays?

I have two arrays as shown in the picture.

//array 1
Array
(
    [0] => 223
    [1] => 216
)

/array 2
Array
(
    [221] => Bakers
    [220] => Construction
    [223] => Information Technology
    [216] => Jewellery
    [217] => Photography
    [222] => Retailers
)

      

I want text where the key (values) of the first array matches the second array (keys).

Expected Result:

Information Technology, Jewellery

      

+3


source to share


2 answers


$result = array();
foreach( $array1 as $index ) {
  $result[] = $array2[ $index ];
}
echo implode( ', ', $result );

      



+2


source


Just get the array_intersect_key()

keys, but since you have the keys as values โ€‹โ€‹in the first array, you need to flip it with array_flip()

, for example



print_r(array_intersect_key($array2, array_flip($array1)));

      

+6


source







All Articles