PHP:检查的价值和关键的多维数组存在 [英] PHP: Check if value and key exist in multidimensional array
问题描述
只是想知道,如果你能够给我一个手这样的:
Just wondering if you can give me a hand with this:
我有一个多维数组...
I have a multidimensional array...
$my_array = array(
0 => array(
"name" => "john",
"id" => 4
),
1 => array(
"name" => "mark",
"id" => 152
),
2 => array(
"name" => "Eduard",
"id" => 152
)
);
什么是检查,如果数组$ my_array包含关键字ID,其值152.我不需要回应,或使用任何值的任意值,最快和最有效的方式的想法。我只需要它来检查(返回true),如果数组有ID为钥匙和值152的任何记录。
Any idea about what would be the fastest and most efficient way to check if the array $my_array contains any value with the key "id" and its value 152. I don't need to echo or use any of the values. I just need to check (return true) if the array has any records with the ID "key" and the value "152" in it.
推荐答案
没有什么会比一个简单的循环速度更快。您可以混合和匹配一些阵列功能来做到这一点,但他们只会被实现为一个循环了。
Nothing will be faster than a simple loop. You can mix-and-match some array functions to do it, but they'll just be implemented as a loop too.
function whatever($array, $key, $val) {
foreach ($array as $item)
if (isset($item[$key]) && $item[$key] == $val)
return true;
return false;
}
这篇关于PHP:检查的价值和关键的多维数组存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!