By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,709 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,709 IT Pros & Developers. It's quick & easy.

Using function array_intersect_key

zorgi
Expert 100+
P: 431
I have a question but not a problem.

I have 2 arrays.

Expand|Select|Wrap|Line Numbers
  1. $array_1 = array("a", "b");
  2. $array_2 = array("a" => "x1", "b" => "x2", "c" => "x3");
  3.  
Is there php function that would return array("a" => "x1", "b" => "x2") if passed $array_1 and $array_2 or I have to write it. Looked at the manual and couldn't find it.

Thanks
Sep 24 '09 #1
Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you donít need a whole new functionÖ
Expand|Select|Wrap|Line Numbers
  1. array_intersect_key($array_2, array_flip($array_1));
Sep 24 '09 #2

code green
Expert 100+
P: 1,726
Try array_intersect_key() for this one
array array_intersect_key ( array array1, array array2 [, array ...] )

array_intersect_key() returns an array containing all the values of array1 which have matching keys that are present in all the arguments.
But you will need to array_flip() $array1 to make values the key. So...
Expand|Select|Wrap|Line Numbers
  1. array_intersect_key($array2,array_filp($array1);
..I think
Sep 24 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
@code green
Iíve tested that, it works.
Sep 24 '09 #4

zorgi
Expert 100+
P: 431
Excellent

Thank you
Sep 24 '09 #5

code green
Expert 100+
P: 1,726
Dormilich. You pipped me by one minute. But same solution.
Sep 24 '09 #6

Dormilich
Expert Mod 5K+
P: 8,639
itís the obvious solution, after all.
Sep 24 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.