通过键获取数组值 [英] Get array values by keys

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

问题描述

我正在寻找一个内置的 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);

而且我需要 key2key4 键的值,所以我有另一个 array("key2", "key4")我需要一个函数,它将这个数组和第一个数组作为输入,并为我提供响应值.所以响应将是 array(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);

你可以使用 array('a' => '', 'c' => '') 而不是 array_flip(...) 如果你想要更简单的代码.

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

注意数组键被保留.如果你需要一个顺序数组,你应该在之后使用 array_values.

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

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

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