4

I've done a bunch of searching but can't figure this one out.

I have an array like this:

$array = array(cat => 0, dog => 1);

I have a string like this:

I like cats.

I want to see if the string matches any keys in the array. I try the following but obviously it doesn't work.

array_key_exists("I like cats", $array)

Assuming that I can get any random string at a given time, how can I do something like this?

Pseudo code:

array_key_exists("I like cats", *.$array.*)
//The value for cat is "0"

Note that I want to check if "cat" in any form exists. It can be cats, cathy, even random letter like vbncatnm. I am getting the array from a mysql database and I need to know which ID cat or dog is.

Keith C.
  • 365
  • 1
  • 5
  • 15

3 Answers3

7

You can use a regex on keys. So, if any words of your string equal to the key, $found is true. You can save the $key in the variable if you want. preg_match function allows to test a regular expression.

$keys = array_keys($array);
$found = false;
foreach ($keys as $key) {
    //If the key is found in your string, set $found to true
    if (preg_match("/".$key."/", "I like cats")) {
        $found = true;
    }
}

EDIT :

As said in comment, strpos could be better! So using the same code, you can just replace preg_match:

$keys = array_keys($array);
$found = false;
foreach ($keys as $key) {
    //If the key is found in your string, set $found to true
    if (false !== strpos("I like cats", $key)) {
        $found = true;
    }
}
Anthony
  • 2,014
  • 2
  • 19
  • 29
  • Thanks Anthony. When I try this I get "Warning: preg_match(): Delimiter must not be alphanumeric or backslash in /var/app/current/reporting/SqsToDbWorker.php on line 56" Not sure why it must NOT be alphanumeric? – Keith C. Oct 22 '16 at 19:25
  • I'm sorry that was my fault, I fix the issue. In preg_match we have to surround pattern ($key, here) by a delimiter. I added it but you can use a sharp if you want why not. – Anthony Oct 22 '16 at 19:30
  • I think regular expression are a little too resource-heavy for this kind of thing. You can just use `strpos` – Cave Johnson Oct 22 '16 at 19:33
  • Yup that fixed it. Thanks. Note, you have a period before the first "/" which doesn't belong. Fix that for anyone who references this in the future. – Keith C. Oct 22 '16 at 19:33
  • @KodosJohnson that is correct, php documentation suggests the same thing. – Keith C. Oct 22 '16 at 19:34
  • @KodosJohnson you are right, so I added strpos into my answer. – Anthony Oct 22 '16 at 19:39
  • Can you do `if (strpos("I like cats", $key) !== false)` instead? The reason is that if `cats` is in the beginning of the string, `strpos` will return 0 since it returns the position number and that is falsey, so it will evaluate to false even if the string is there. – Cave Johnson Oct 22 '16 at 19:42
  • I just did it, thanks. Indeed strpos returns the position you are right. And if strpos does not find the string it returns a *falsy* value, so check with !== is important as said in documentation. – Anthony Oct 22 '16 at 19:45
  • @AnthonyB One final note .. it wasn't finding everything while running a test so I did strpos("/I like cats/", $key) which seemed to fix it. I'm not sure if it is good for to put regular expression here but it did fix it. – Keith C. Oct 22 '16 at 19:58
  • @KeithC. can you give me an exemple ? I'll try to fix it if there is a bug. – Anthony Oct 23 '16 at 11:07
2

This should help you achieve what you're trying to do:

$array         = array('cat' => 10, 'dog' => 1);

$findThis      = 'I like cats';

$filteredArray = array_filter($array, function($key) use($string){

    return strpos($string, $key) !== false;

}, ARRAY_FILTER_USE_KEY);

I find that using the array_filter function with a closure/anonymous function to be a much more elegant way than a foreach loop because it maintains one level of indentation.

useyourillusiontoo
  • 1,287
  • 1
  • 10
  • 24
0

You could do using a preg_match with the value not in array but in search criteria

 if(preg_match('~(cat|dog)~', "I like cats")) {
    echo 'ok';
}

or

$criteria = '~(cat|dog)~';

 if (preg_match($criteria, "I like cats")) {
    echo 'ok';
}

Otherwise you could use a foreach on your array

 foreach($array as $key => $value ) {
     $pos = strpos("I like cats", $key);
     if ($pos > 0) {
      echo $key .  ' '. $value;
     }

 }
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Understood. However, I am getting the array from a database and I need to know which ID car or dog is. – Keith C. Oct 22 '16 at 18:55