0

I am trying to check for values in array and if value found increment it. I've tried to do it as shown in code below, but not successful.

$productdas=array("DAS","DayEnd","DAAS");
if (strpos(serialize($row['pirority']),"P1")!==false &&
   strpos(serialize($row['product']),'$productdas')!==false) 
       { 
         $dasp1++; 
       }

I'll be grateful for any help.

Regards.

3
  • What does $row['pirority'] contain and what is the expected result? Commented Apr 15, 2014 at 5:50
  • Why are you using serialize()? Commented Apr 15, 2014 at 5:50
  • 1
    '$productdas' does not parse the variable. Lose the 's Commented Apr 15, 2014 at 5:51

2 Answers 2

1

You would need to write an strpos() variant that accepts an array as $needle; for example:

function strpos_array($haystack, array $needles)
{
    foreach ($needles as $needle) {
        if (($pos = strpos($haystack, $needle)) !== false) {
            return $pos;
        }
    }
    return false;
}

if (strpos_array(serialize($row['product']), $productdas)!==false) { ... }

It's also possible to implement this using preg_match().

Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming you're trying to search the given string for any of the values in $productas array and find the number of total occurrences. This can be done with substr_count():

$productdas = array("DAS","DayEnd","DAAS");
$count = 0;

foreach ($productdas as $needle) {
    $count += substr_count($row['pirority'], $needle);
}

echo $count;

If $row['pirority'] is DASfooDayEndDAAShelloDAS, then the count would be 4.

Demo

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.