要确保PHP中in_array的准确性,可以遵循以下几个建议:
$search = '123';
$array = array('123', 456, '789');
if (in_array($search, $array)) {
echo "找到了!";
} else {
echo "没找到!";
}
$search = '123';
$array = array('123', 456, '789');
if ($search === $array[0]) {
echo "找到了!";
} else {
echo "没找到!";
}
$search = 'b';
$array = array('a' => 1, 'b' => 2, 'c' => 3);
$flippedArray = array_flip($array);
if (in_array($search, $flippedArray)) {
echo "找到了!";
} else {
echo "没找到!";
}
$search = 'b';
$array = array('a' => 1, 'b' => 2, 'c' => 3);
if (array_search($search, $array)) {
echo "找到了!";
} else {
echo "没找到!";
}
$array = array('d' => 4, 'a' => 1, 'b' => 2, 'c' => 3);
asort($array);
if (in_array('b', $array)) {
echo "找到了!";
} else {
echo "没找到!";
}
遵循以上建议,可以确保PHP中in_array函数的准确性。