我想搜索一个字符串并获取相关值,但在测试函数时,每次搜索单词(Title
或Would
或Post
或Ask
)显示(给出)一个输出Title,11,11
!!!!如何解决?
// test array
$arr = array('Title,11,11','Would,22,22','Post,55,55','Ask,66,66');
// define search function that you pass an array and a search string to
function search($needle,$haystack){
//loop over each passed in array element
foreach($haystack as $v){
// if there is a match at the first position
if(strpos($needle,$v) == 0)
// return the current array element
return $v;
}
// otherwise retur false if not found
return false;
}
// test the function
echo search("Would",$arr);