检查多维数组中是否存在特定的数组键 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19420715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if specific array key exists in multidimensional array - PHP
提问by Darren Sweeney
I have a multidimensional array e.g. (this can be many levels deep):
我有一个多维数组,例如(这可以有很多层次):
$array = Array (
[21] => Array ( )
[24] => Array (
[22] => Array ( )
[25] => Array (
[26] => Array ( )
)
)
)
I am trying to loop through it to see if a certain key exists:
我正在尝试遍历它以查看某个键是否存在:
$keySearch = 22; // key searching for
function findKey($array, $keySearch) {
foreach ($array as $item){
if (isset($item[$keySearch]) && false === findKey($item[$keySearch], $item)){
echo 'yes, it exists';
}
}
}
findKey($array, $keySearch);
But it finds nothing. Is there an error in the loop?
但它什么也没找到。循环中是否有错误?
回答by Tim
array_key_exists()
is helpful.
array_key_exists()
是有帮助的。
Then something like this:
然后是这样的:
function multiKeyExists(array $arr, $key) {
// is in base array?
if (array_key_exists($key, $arr)) {
return true;
}
// check arrays contained in this array
foreach ($arr as $element) {
if (is_array($element)) {
if (multiKeyExists($element, $key)) {
return true;
}
}
}
return false;
}
Working example: http://codepad.org/GU0qG5su
工作示例:http: //codepad.org/GU0qG5su
回答by Alexandre Nucera
I played with your code to get it working :
我玩过你的代码让它工作:
function findKey($array, $keySearch)
{
foreach ($array as $key => $item) {
if ($key == $keySearch) {
echo 'yes, it exists';
return true;
} elseif (is_array($item) && findKey($item, $keySearch)) {
return true;
}
}
return false;
}
回答by knot22
Here is a one line solution:
这是一个单行解决方案:
echo strpos(json_encode($array), $key) > 0 ? "found" : "not found";
This converts the array to a string containing the JSON equivalent, then it uses that string as the haystack argument of the strpos() function and it uses $key as the needle argument ($key is the value to find in the JSON string).
这会将数组转换为包含等效 JSON 的字符串,然后使用该字符串作为 strpos() 函数的 haystack 参数,并使用 $key 作为needle 参数($key 是要在 JSON 字符串中查找的值)。
It can be helpful to do this to see the converted string: echo json_encode($array);
这样做有助于查看转换后的字符串: echo json_encode($array);
Be sure to enclose the needle argument in single quotes then double quotes because the name portion of the name/value pair in the JSON string will appear with double quotes around it. For instance, if looking for 22 in the array below then $key = '"22"'
will give the correct result of not foundin this array:
确保用单引号和双引号括起针参数,因为 JSON 字符串中名称/值对的名称部分将用双引号括起来。例如,如果在下面的数组中查找 22$key = '"22"'
则将给出在此数组中找不到的正确结果:
$array =
Array (
21 => Array ( ),
24 =>
Array (
522 => Array ( ),
25 =>
Array (
26 => Array ( )
)
)
);
However, if the single quotes are left off, as in $key = "22"
then an incorrect result of foundwill result for the array above.
但是,如果不使用单引号,$key = "22"
则上面的数组将导致found的错误结果。
EDIT:A further improvement would be to search for $key = '"22":';
just incase a value of "22"
exists in the array. ie. 27 => "22"
In addition, this approach is not bullet proof. An incorrect foundcould result if any of the array's values contain the string '"22":'
编辑:进一步的改进是搜索是否存在数组中$key = '"22":';
的值"22"
。IE。27 => "22"
此外,这种方法不是防弹的。如果任何数组的值包含字符串,则可能会导致错误的发现'"22":'
回答by minboost
function findKey($array, $keySearch)
{
// check if it's even an array
if (!is_array($array)) return false;
// key exists
if (array_key_exists($keySearch, $array)) return true;
// key isn't in this array, go deeper
foreach($array as $key => $val)
{
// return true if it's found
if (findKey($val, $keySearch)) return true;
}
return false;
}
// test
$array = Array (
21 => Array ( 24 => 'ok' ),
24 => Array (
22 => Array ( 29 => 'ok' ),
25 => Array (
26 => Array ( 32 => 'ok' )
)
)
);
$findKeys = Array(21, 22, 23, 24, 25, 26, 27, 28, 29, 30);
foreach ($findKeys as $key)
{
echo (findKey($array, $key)) ? 'found ' : 'not found ';
echo $key.'<br>';
}
回答by Luke Snowden
returns false if doesn't exists, returns the first instance if does;
如果不存在则返回 false,如果存在则返回第一个实例;
function searchArray( array $array, $search )
{
while( $array ) {
if( isset( $array[ $search ] ) ) return $array[ $search ];
$segment = array_shift( $array );
if( is_array( $segment ) ) {
if( $return = searchArray( $segment, $search ) ) return $return;
}
}
}
return false;
}
回答by Jelle Ferwerda
For sure some errors, is this roughly what you are after? (Untested code):
肯定有一些错误,这大致是您所追求的吗?(未经测试的代码):
$keySearch=22;
// key seraching for
$keySearch=22;
// 密钥搜索
function findKey($array, $keySearch)
{
// check whether input is an array
if(is_array($array)
{
foreach ($array as $item)
{
if (isset($item[$keySearch]) || findKey($item, $keysearch) === true)
{
echo 'yes, it exists';
return true;
}
}
}
}