0

I am having a a bit of an issue related to checking a returned array for specific matches.

Here is what I do:

I query a server API and the API returns a printable result using:

print_r($result);

the printed result is:

Array
(
    [<html><center>Password_Saved!</center></html>] => 
)

So I thought I could do something like:

function checkResult ($needle, $haystack) {   return ( stripos($haystack,$needle) !== false ? TRUE : FALSE); }

if ((checkResult("saved",$result))) {
echo "saved";
} else {
echo "not saved";
}

However, this does not work at all, so I am wondering if you could help me find a way if the $result contains the string saved since I need to know this to perform the next action based on the result.

Your help would be greatly appreciated.

Marcus Weller
  • 361
  • 1
  • 3
  • 11
  • 2
    `stripos` expects a *string* as the haystack. You pass in *an array*... It gets silently converted to the string `"Array"` (and emits a warning in more recent versions of PHP). – DCoder Aug 17 '13 at 14:07

3 Answers3

2

The value you are looking for exists in the array's key instead of value. As such, you need to be doing your search in the array's keys instead of values.

foreach ($result as $key => $value)
{
   if (false !== stripos ($key, "saved"))
   {
      print "{$key} => Saved";
   }
}
Sutandiono
  • 1,748
  • 1
  • 12
  • 21
0

Read about php preg_grep function

For example:

$needle_pattern = '/search/i'; // i for case insensitive
preg_grep($needle_pattern, $array_haystack);

Also notice that in your code you're mixing "saved" and "Saved" which are different :)

For further reading about this method:

How to search in an array with preg_match?

P.S If your "haystack" is actually the keys, you can switch $array_haystack with array_keys($array_haystack) to get an array of all keys.

Community
  • 1
  • 1
Itay
  • 16,601
  • 2
  • 51
  • 72
  • This will not work, as `saved` is only a partial match. Also, looking at the printed output he has provided, it looks like `
    Password_Saved!
    ` is actually the array **key**, not its value.
    – BenM Aug 17 '13 at 14:10
  • Again, please check the output. This isn't about searching the value of an array, but rather its keys... – BenM Aug 17 '13 at 14:13
  • Again, fixed (last comment) – Itay Aug 17 '13 at 14:14
0

it doesn't work because your searching for saved when the value you have in the string is Saved. and you should pass your array key.

if ((checkResult("Saved",array_keys($result)[0]))) {
echo "saved";
} else {
echo "not saved";
}
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • 1
    Please check the output of the `$result` array the OP has provided. It looks like `
    Password_Saved!
    ` is actually the array **key**, not its value. Also, this will not return true for a partial match, which is what the OP is asking for.
    – BenM Aug 17 '13 at 14:11
  • No good after the edit, either. `stripos` expects a string, not an array. – BenM Aug 17 '13 at 14:14
  • @BenM i don't understand what you mean by match will only be partial – DevZer0 Aug 17 '13 at 14:17
  • 1
    My bad, didn't realize you were passing the first array key in, I thought you were passing the whole array through. – BenM Aug 17 '13 at 14:20