如何在数组中搜索部分字符串匹配并返回数组中第一次出现的键。 以下示例
$keys = array(0 => 'Industrial acids', 1 => 'Industrial chemicals', 2 => 'ovens', 3 => 'Chemical companies');
搜索Indus必须返回0
Industrial must return 0
chemi must return 1
Industrial chemi must return 1
答案 0 :(得分:0)
您可以使用简单的foreach
<?php
$index = 0;
$searchedString='chemic';
foreach($keys as $key => $value){
if ($value == $searchedString){ //If you are searching the exact String $searchedString (here 'chemics'
$index = $key;
break; //To be sure it will be the first index
}
}
echo $index;
?>
如果您只想知道字符串中是否有“chemic”,请使用strpos($value,$searchedString) !== false
代替$value == $searchedString