要確保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函數的準確性。