0

Here is the code

$description = explode("<li>", $rows['description']);
var_dump($description);
$find = 'annual';
$key = array_search($find, $description);
var_dump($key);
//echo $description[$key];

Here is the output:

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)

Variable $find is searching for "annual" in the output you can see array key 1 has annual in it, but it's returning false.

So I don't know what I am missing or doing wrong. I've tried testing it with the whole value of array 1 to make sure it wasn't a problem searching inside the array, still got false. Also changed $find = "Generous" same result...False

1 Answer 1

3

You are misunderstanding the function of array_search(). The following line:

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.*

Contains the string annual, but it is not the string annual in its entirety. In other words array_string() does not search inside the strings, it attempts an exact match.

To find the result you are looking for I would trey something like the following:

$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);

This iterates over the $description array and returns an array $filtered containing any elements that contain the string annual. Alternatively you could use a foreach loop and add each matching example to a $matches array.

Example here: https://eval.in/146145

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Darragh, if that is the case, why does this return false. $find = "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.*", because I figured since it wasn't working by searching inside, I would try the entire value, but that is still false as well. Regardless, I'm working in your solution now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.