Sagar Singh Sagar Singh - 7 months ago 28
PHP Question

Match a string in an array strpos(): Empty needle

I have a string from database that I want to match with an array but it results in error saying:


strpos(): Empty needle in line X


if (!isset($_SESSION['arry'])) {
$_SESSION['arry'] = array();
}
$imp = 42;
$arrys = $_SESSION['arry'];

foreach($arrys as $string)
{
if(strpos($imp, $string) !== false)
{
$pow =1;
break;
}
}

if($pow==1){ exit; }


The array looks like this:

Array
(
[0] => 218
[1] => 219
[2] => 218
[3] => 220
[4] => 222
[5] => 42
[6] => 223
)

Answer

You are doing the opposite way , searching string in 42 , instead go for

strpos($string, $imp) 

and as @abracadaver added you can use in_array

TRY THIS : (updated)

foreach($arrys as $string)
{
  if(strpos($string, $imp) !== FALSE) 
  {
    $pow =1;
    break;
  }
}
Comments