找按键数组值 [英] Get array values by keys

查看:87
本文介绍了找按键数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个内置的PHP函数,它的键数组作为输入,并返回相应的我的价值观。

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

有关如我有以下数组

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

和我需要值键键2 键4 所以我有另一个阵列(KEY2,KEY4)
我需要一个功能,借此阵列和第一阵列作为输入,并提供我值响应。所以反应会阵列(200,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)

推荐答案

我认为你正在寻找 array_intersect_key 。例如:

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')));

将返回:

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

UPD:您可以使用阵列('A'=>'','C'=>'')而不是 array_flip(...)如果你想有一个稍微简单code。您也可以使用 array_values​​ 毕竟,如果你不需要有键preserved。

UPD: You may use array('a' => '', 'c' => '') instead of array_flip(...) if you want to have a little simpler code. You may also use array_values after all if you don't need to have keys preserved.

这篇关于找按键数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆