我正在努力解释我想在这里做什么,所以如果我让你感到困惑,我深表歉意。我自己也同样困惑
我有一个像这样的数组:
$foo = array(
array('value' => 5680, 'text' => 'Red'),
array('value' => 7899, 'text' => 'Green'),
array('value' => 9968, 'text' => 'Blue'),
array('value' => 4038, 'text' => 'Yellow'),
)
我想检查数组是否包含值,例如7899 并获取链接到上例中该值“Green”的文本。
尝试这样的事情
$foo = array(
array('value' => 5680, 'text' => 'Red'),
array('value' => 7899, 'text' => 'Green'),
array('value' => 9968, 'text' => 'Blue'),
array('value' => 4038, 'text' => 'Yellow'),
);
$found = current(array_filter($foo, function($item) {
return isset($item['value']) && 7899 == $item['value'];
}));
print_r($found);
哪个输出
Array
(
[value] => 7899
[text] => Green
)
这里的关键是array_filter
。如果搜索值7899
不是静态的那么你可以用类似的东西将它带入闭包中function($item) use($searchValue)
。注意array_filter
返回一个元素数组,这就是我传递它的原因current
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)