3

Hi! I want to search string having value like "CMS" in array $row, below code is searching for exact "CMS".

if (mysql_num_rows($sql_result)>0)
  { 
    while ($row = mysql_fetch_assoc($sql_result))
        { $counter++; 
           if (in_array("CMS", $row)) 
               { $cms++; }
          }
    }

I'll be grateful if any one can guide me to how to search string for value LIKE "CMS" in array $row.

MOZ
  • 768
  • 4
  • 19
  • 33

3 Answers3

3

Use strpos and serialize

if (mysql_num_rows($sql_result)>0)
  { 
    while ($row = mysql_fetch_assoc($sql_result))
        { $counter++; 
           if (strpos(serialize($row),"CMS")!==false)  //<--- Changed this `if` statement, nothing else
               { $cms++; }
          }
    }

You could serialize your array and then do a strpos to check whether the needle is inside the array or not. This is something similar like doing a LIKE as you requested.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

Loop over each of the columns in the row and use strpos

if (mysql_num_rows($sql_result)>0) 
{ 
    while ($row = mysql_fetch_assoc($sql_result))
    { 
        $counter++; 
        foreach($row as $column)
        {
            if (strpos($column, 'CMS'))
            {
                $cms++;
            }
        }
    }
}
Tom Griffin
  • 435
  • 4
  • 9
0
if (mysql_num_rows($sql_result)>0)
  { 
    while ($row = mysql_fetch_assoc($sql_result))
        { $counter++; 
           foreach ($row as $column) {
              if (strpos($column, "CMS") !== false) {
                  $cms++;
              }
           } 

        }
    }
e-fisher
  • 659
  • 1
  • 6
  • 8