Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am searching for a built in php function that takes array of keys as input and returns me corresponding values.

for e.g. I have a following array

$arr = array("key1"=>100, "key2"=>200, "key3"=>300, 'key4'=>400);

and I need values for the keys key2 and key4 so I have another array("key2", "key4") I need a function that takes this array and first array as inputs and provide me values in response. So response will be array(200, 400)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
317 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think you are searching for array_intersect_key. Example:

array_intersect_key(array('a' => 1, 'b' => 3, 'c' => 5), 
                    array_flip(array('a', 'c')));

Would return:

array('a' => 1, 'c' => 5);

You may use array('a' => '', 'c' => '') instead of array_flip(...) if you want to have a little simpler code.

Note the array keys are preserved. You should use array_values afterwards if you need a sequential array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...