0

I have an array

$op1 = array('45'=>123, '657'=>65, '6578'=>234, '65782'=>0, '3'=>25, '756'=>156, '3445'=>178, '20924'=>92);

And then a variable taken from a form that I want to see if any of the array keys starts with it.

Variable: $nr=$_POST['number'];

Code:

    foreach (array_keys($op1) as $key) {
     if(preg_match('/$nr/', $key))
     {
       echo "FOUND";
     }
     else{
       echo "NOT FOUND";
     }

But it never finds anything in the array. I know that I am probably doing something wrong, but don't know what.

user3329074
  • 127
  • 9
  • 1
    Variables are expanded inside double quotes, not inside single quotes. This is basic PHP syntax, having nothing to do with preg_match. – Barmar Feb 19 '15 at 00:54
  • If you want to check whether the key _starts_ with a number, don't forget to use the `^` anchor in the regexp. – Barmar Feb 19 '15 at 00:57
  • Had to be something really basic that I wasn't noticing. Thank you. – user3329074 Feb 19 '15 at 19:02

0 Answers0