我希望得到数组的所有键,将每个键与数字进行比较,这样的事情:
array(
[0] => 7
[1] => 8
[2] => 4
[3] => 6
)
if (6 != EACH KEY OF ARRAY) {
so...
}
Run Code Online (Sandbox Code Playgroud)
条件不会显示,因为有[3] => 6,当然键6 = 6.
有功能吗?别的什么?
.
foreach($array as $key => $val)
{
if (6 != $key) {
// so...
}
}
Run Code Online (Sandbox Code Playgroud)
例:
$array = array(7, 8, 4, 6);
foreach($array as $key => $val)
{
if (6 != $key) {
echo '6 is not equal to ' . $key . '<br />';
}
else {
echo '6 is equal to ' . $key . '<br />';
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
6 is not equal to 7
6 is not equal to 8
6 is not equal to 4
6 is equal to 6
Run Code Online (Sandbox Code Playgroud)
但是,如果要检查数组中是否存在6 的值,请使用in_array如下所示:
if (in_array(6, $array)) {
// 6 is present in the array
}
Run Code Online (Sandbox Code Playgroud)