如果我有
$tallyArray = Array
(
["Group1"] => Array
(
["Item1"] => 2
["Item2"] => 1
)
["Group2"] => Array
(
["Item1"] => 1
["Item2"] => 1
)
)
我想知道“Group1”是否已经存在。哪个是用变量$grp = "Group1"
测试的。你怎么做。
我试过了:
$tas = sizeof($tallyArray);
$grpPOS = 0;
$grpFound = 'not found';
for($z=0; $z <= $tas; $z++){
if($tallyArray[$z] == $grp){
$grpFound="found";
$grpPOS = $z;
}
}
我试过
$grpFound = in_array_recursive($grp, $tallyArray) ? 'found' : 'not found';
我试过
$grpFound = in_array($grp, $tallyArray) ? 'found' : 'not found';
但是没有这些回归“发现”。
请帮忙。
答案 0 :(得分:1)
使用array_key_exists
检查数组中是否存在给定的键或索引。
if ( array_key_exists($grp, $tallyArray) ) {
// do something
}