3

How to match array key with array value. With array_intersect() is to match key array. But how to match key in first array with value in second array.

For example array:

$value_array=array(
'1'=>'text one',
'2'=>'text two',
'3'=>'text three',
'4'=>'text four',
'5'=>'text five',
'6'=>'text six',
'7'=>'text seven',
'8'=>'text eight',
'9'=>'text nine',
'10'=>'text ten'
);

$key_array=array(
'1'=>'1',
'2'=>'2',
'3'=>'4',
'4'=>'5',
'5'=>'7'
);

if use array_intersect(), is use to match key. I want to search key array and get value array. And output will show like this:

Array (
        [1] => text one 
        [2] => text two 
        [3] => text four 
        [4] => text five 
        [5] => text seven 
      )
1
  • Its duplicate of this Commented Jan 27, 2016 at 7:07

1 Answer 1

1

I got the answer, here it is :

print_r(array_intersect_key($value_array, array_flip($value_key)));

and output will show:

    Array ( 
[1] => text one 
[2] => text two 
[4] => text four 
[5] => text five 
[7] => text seven 
)
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure if this is really a good way to do this. array_flip is adding extra load. Instead you can achieve using one single loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.