PHP在数组中只是数组元素的开头


PHP in array for just start of array element

我有这个代码:

$array = array("Keys,placeholder", "Cheese,placeholder", "Knees,placeholder");
if(in_array("Cheese", $array)) {
    print("We do indeed have cheese!");
}
else {
    print("No cheese, Gromit!");
}

但当它检查数组时,它会检查整个数组是否为"Cheese",而不仅仅是它是否包含文本"Cheese"。

有办法实现我想要的吗?

编辑:此外,我只想在第一位包含奶酪的情况下触发代码;所以对于这个例子:

$array = array("Keys,placeholder", "Placeholder,cheese", "Knees,placeholder");
if(in_array("Cheese", $array)) {
    print("We do indeed have cheese!");
}
else {
    print("No cheese, Gromit!");
}

我不希望代码被触发。

implode it:

if (strpos(implode('', $array),'Cheese')!==false) print("We do indeed have cheese!");
else print("No cheese, Gromit!");

好的,奶酪第一,去

... implode('|', $array), '|Cheese') ...
if (preg_match("/Cheese/i", $array)) {
  return true;
}

试试这个