Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search string for value using array in PHP? [duplicate]

Tags:

arrays

string

php

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.

like image 698
MOZ Avatar asked Jan 25 '26 14:01

MOZ


1 Answers

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.

like image 53
Shankar Narayana Damodaran Avatar answered Jan 28 '26 05:01

Shankar Narayana Damodaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!