这是代码
$description = explode("<li>", $rows['description']);
var_dump($description);
$find = 'annual';
$key = array_search($find, $description);
var_dump($key);
//echo $description[$key];
这是输出:
array(9) {
[0]=> string(4) " "
[1]=> string(185) "Fair. No annual fee. No overlimit fee. No foreign transaction fee. Pay up to midnight ET online or by phone on your due date without a fee. Plus, paying late won't raise your APR.*"
[2]=> string(183) "Generous. 5% cash back at Home Improvement Stores & More on up to $1,500 in purchases from April through June 2014 when you sign up. And 1% cash back on all other purchases.*"
[3]=> string(64) "Human. 100% U.S.-based customer service available any time."
[4]=> string(188) "Looks out for you-since each Discover purchase is monitored. If it's unusual, you're alerted by e-mail, phone or text-and never responsible for unauthorized Discover card purchases.*"
[5]=> string(106) "Plus, free FICO® Credit Score on your monthly statement to help you stay on top of your credit.*"
[6]=> string(171) "0% Intro APR* on balance transfers for 18 months. Then the variable purchase APR applies, currently 10.99% - 22.99%. A fee of 3% applies for each balance transferred."
[7]=> string(112) "0% Intro APR* on purchases for 6 months. Then the variable purchase APR applies, currently 10.99% - 22.99%."
[8]=> string(119) "*Click "Apply" to see rates, rewards, and free FICO® Credit Score terms and other information.
" }
bool(false)
变量$find
正在输出中搜索"annual"
,您可以看到数组键1中包含年份,但它返回 false 。
所以我不知道自己错过了什么或做错了什么。我已经尝试使用数组1的全部值来测试它,以确保在数组内搜索没有问题,但仍然是错误的。同样更改$find = "Generous"
相同的结果...错误
答案 0 :(得分:3)
您误解了array_search()
的功能。以下一行:
公平。没有年费。没有超额费用。没有国外交易费。在线收费至午夜,或在您的截止日期通过电话付费,无需付费。另外,延迟付款不会提高你的APR。*
包含字符串annual
,但不是字符串annual
。换句话说,array_string()
不会在字符串中搜索,而是尝试完全匹配。
要查找您要查找的结果,我会发现以下内容:
$matches = array_filter($description, function($el) {
// evaluate the current element
// return true if a string index is
// found for the target string
return strpos($el, 'annual') !== false;
});
var_dump($matches);
迭代$description
数组并返回包含任何包含字符串$filtered
的元素的数组annual
。或者,您可以使用foreach
循环并将每个匹配的示例添加到$matches
数组。
希望这有帮助。