Manish Prajapati
Manish Prajapati

Reputation: 1641

How can I get the key intersect of two arrays?

I have two arrays as shown blow

//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 that text where key (values) of first array matches to second array (keys).

expected result:

Information Technology, Jewellery

Upvotes: 3

Views: 76

Answers (2)

martynasma
martynasma

Reputation: 8595

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

Upvotes: 2

Rizier123
Rizier123

Reputation: 59701

Just get the array_intersect_key() of the keys, but since you have the keys as values in the first array you have to flip it with array_flip(), e.g.

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

Upvotes: 6

Related Questions