0

Ok posting this query after going through many similar issues but they took into consideration only numeric values. So my issue is:

I have 2 arrays -

$a = ["XYZ1250H100",
"XYZ1280H130",
"XYZ1250H150",
"XYZ3300H200",
"XYZ3350H200",
"XYZ33350H280Ada08",
"XYZ33450H300Ada08",
"XYZ33508H406Ada08"]; 

and

$b = ["XYZ0L200H150A4c00",
    "350L610H457Ada08",
    "XYZ33762H610Ada08",
    "350L914H610Ada08",
    "3700L250H200A410b",
    "XYZ33457H305Ada08",
    "XYZ4550H100MMQOJ",
    "XYZ4580H130Ada08",
    "XYZ4550H150A69b5",
    "3101L280H356A8b83",
    "XYZ4550H1501FC5Z",
    "3116L150H15074QFR",
    "XYZ1250H200A21ac",
    "3101L750H500A8b83",
    "350L356H279Ada08",
    "XYZ1250H200A3f1c",
    "3700L153H102A8d96",
    "XYZ4550H150Ada08",
    "XYZ4580H130A69b5",
    "350L1830H610Ada08",
    "3700L153H102A4c00",
    "XYZ4550H150STD9J",
    "3800L200H1505CZJI",
    "XYZ4550H100A69b5",
    "XYZ331370H450Ada08"
  ];

I need to see if values of array $a are there in array $b, I tried to use array_diff() and array_intersect() but that didn't solve the purpose. What I want is if the 2 arrays are matched then it should return either true or false. So for example if we consider value in $a -> "XYZ1280H130" and value in $b -> "XYZ1280H130A69b5" then it should be considered as a matched value (coz former is a sub-string of latter value) and true should be return and same for all similar values.

I tried something like this but it gives back all values of array $b everytime:

$result = array_filter($a, function($item) use($b) {
  foreach($b as $substring)
    if(strpos($item, $substring) !== FALSE) return TRUE;
  return FALSE;
});

Please let me know if I missed something. Thanks in advance.

2
  • Can you add an example of the output you wanted this comparison to produce? Commented Sep 20, 2017 at 19:11
  • If all values of $a are there in $b then it should return true else if any value is missing from $b then false. Hope this makes sense. Commented Sep 20, 2017 at 19:14

3 Answers 3

1

Use regex:

foreach($b as $search){
      foreach($a as $sub){
       if(preg_match("/$sub/",$search)){ echo "found ".$sub." in ".$search;}
      }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way we can avoid double foreach loop coz am getting to this point of comparison already after couple of loops?
Not really because you need to run through each of the items in a, check against each item in b (or vice versa).
0

You could use array_map. But this should give you the result you want as well :

function test($a, $b) {
    foreach($a as $first) {
        foreach($b as $second) {
            if(strpos($b, $a) !== false) return true;
        }
    }
}

Comments

0

Modified one of the answers above by @clearshot66 to get the desired result:

function match_them($a, $b){    
    foreach($b as $search){
      foreach($a as $key => $val){
       if(preg_match("/$val/",$search)){unset($a[$key]);}
      }
    }   
    return count($a);
}
var_dump(match_them($a, $b));

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.