Search code examples
phparraysregexpreg-match

Php match values between 2 arrays but foe like elements


I know there is array_intersect in php but what I want is to show like elements in two arrays. For example:

array1 = [product, php]
array2 = [product management, html]

i want the resultant array to show result = [product managemenet] array_intersect will give empty result here.


Solution

  • $result=array();
    $array1 = array('product','php');
    $array2 = array('product management','html');
    
    $srcharray = $array2;
    foreach($array1 as $serchstr)
    {
      foreach($srcharray as $key => $serchval)
      {
         if (strpos($serchval, $serchstr) !== false) {
         $result[] = $serchval;
         unset($srcharray[$key]);
         }
      } 
    }
    var_dump($result);
    

    NOTE : If you need case sensitive matching.. use 'stripos' instead of 'strpos'.