0

I'm trying to find a way to check if the value of a given key exists in an array (inside a foreach loop).

For some reason it doesn't really work as expected.

The idea is that I already know the name of the key, but only know part of the correlating value for the key.

I'm trying to do something like this, assuming that I know the key is "exampleKeyName" and its value may or may-not contain "matchValue":

foreach( $array as $key => $value) {
  if (stripos($array["exampleKeyName"], 'matchValue') !== false) { echo "matched"; }
}

This returns "illegal string offset".

However, if I do this it works:

foreach( $array as $key => $value) {
  if (stripos($value, 'matchValue') !== false) { echo "matched"; }
}

The problem here is that I want to be more specific with the query, and only check if the key is "exampleKeyName".

6
  • I don't understand what your problem is : You want to know if a regex pattern matches $array['exampleKeyName'] ? Commented Jun 12, 2014 at 11:35
  • No match the value of that key Commented Jun 12, 2014 at 11:38
  • Why you need to do this check inside foreach loop? Commented Jun 12, 2014 at 11:43
  • @hindmost because the array is inside another array Commented Jun 12, 2014 at 11:45
  • Do you want it with preg_match() Commented Jun 12, 2014 at 11:51

3 Answers 3

1

You can use stripos to check the array value contains in a matching string like this way:

foreach( $array as $key => $value) {
  if (stripos($array[$key], 'matchValue') !== false) { echo "matched"; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user3143218. Great to hear that its worked. Have a nice time.
0

If you want to check if your regex pattern matches a specific value in your array :

if(preg_match("/^yourpattern$/",$myArray["exampleKeyName"])>0) {
    // The value of $myArray["exampleKeyName"] is matched by the regex
}

If you check if the regex matches a key :

foreach($myArray as $key => $value):
    if(preg_match("/^yourpattern$/",$key)>0) {
        // $key is matched by the regex
    }
endforeach;

I hope it helps !

edit : bad english

Comments

0

You're not far off, and there are a few ways of doing this.

In your example code you weren't checking the key but the value in both cases. When doing a for loop like in your second code example you have to change your if statement like so to check against the key:

if (stripos($key, 'matchValue') !== false) { echo "matched"; }

Since I don't know what the application of this is I'll just tell you a few other ways of achieving this and perhaps they will also help simplify the code.

php has a helper function called array_key_exists that checks if the given key or index exists in the array. The result is a boolean value. If you just need to know if the array contains an index or key that is another way of doing this. That would look like this:

if( array_key_exists('matchValue', $array) ) { 
    print('Key exists');
} else {
    print('No key found');
}

There is a good Stack Overflow posting about using preg_grep for regex searching of array keys that may also be helpful if you need to use regular expressions for this.

Comments

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.