0

I am trying to compare one array of values to another array of values. If they intersect, then I would proceed with the rest of my function. However, I can't seem to get array_intersect() to work properly.

Without getting to far into the code, I'll show you the output from debugging my PHP:

intersection: Array ( ) 
needle: Array ( [0] => 37211 [1] => 37212 [2] => 37213 [3] => 37214 ) 
haystack (cpt_codes): Array ( [0] => 70450 [1] => 38221 [2] => 37212 ) 

intersection: Array ( ) 
needle: Array ( [0] => 38221 [1] => G0364 ) 
haystack (cpt_codes): Array ( [0] => 70450 [1] => G0364 [2] => 37212 ) 

intersection: Array ( [0] => 38221 ) 
needle: Array ( [0] => 38221 [1] => G0364 ) 
haystack (cpt_codes): Array ( [0] => 70450 [1] => 38221 [2] => 37212 ) 

In the first example, you will notice that the value "37212" exists in both the needle and haystack, yet the intersection of these two arrays yields no results.

In the second example, you will notice that the value "G0364" exists in both the needle and haystack, yet the intersection of these two arrays yields no results.

In the third example, you will notice that "38221" is in both the needle and haystack, and the function displays the result properly.

Any thoughts? I don't care for the key value, I just need to know if there are ANY intersection values between the two arrays.

Thanks, IP

For completeness, I generated the above output from the following code.

echo "intersection: "; print_r(array_intersect(explode(",",$data[$i][7]), $cpt_codes_test)); echo "<br>";
echo "needle: "; print_r(explode(",",$data[$i][7])); echo "<br>";
echo "haystack (cpt_codes): "; print_r($cpt_codes_test); echo "<br><br>";
Ivan P.
  • 1
  • 2
  • 2
    Can you use `var_dump()` instead of `print_r()` as I can't reproduce the problem, but it may be spaces etc. – Nigel Ren Nov 14 '19 at 19:20
  • Can you restructure your code? it's not easy to read – Marvin Collins Nov 14 '19 at 19:30
  • 2
    Could you include a snippet of _runnable_ code where we can see this happening? – Patrick Q Nov 14 '19 at 19:36
  • @NigelRen - thank you for the suggestion. You were correct, somehow some spaces got in there. I would have thought the explode command would have trimmed spaces from either side of the string. Now I just need to write a quick function to traverse the exploded array and strip the white spaces! – Ivan P. Nov 14 '19 at 20:03
  • Check out https://stackoverflow.com/questions/5762439/how-to-trim-white-spaces-of-array-values-in-php. – Nigel Ren Nov 14 '19 at 20:23
  • 1
    [This](https://stackoverflow.com/questions/19347005/how-can-i-explode-and-trim-whitespace) would be even more relevant. – Patrick Q Nov 14 '19 at 20:25

1 Answers1

-2

Well I do not know what are you doing but it worked for me:

<?php

$arr1 = [37211,37212,37213,37214];
$arr2 = [70450,38221,37212];

$arr3 = array_intersect($arr1, $arr2);

var_dump($arr3);

Output:

array(1) {
  [1]=> int(37212)
}

Also you show us just values but never the code nor you mention which version of PHP are you using with could help us to help you. Keep that in mind for your next questions. Check here

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • 1
    Why not flag as non-repro? – Patrick Q Nov 14 '19 at 19:27
  • @PatrickQ why not add an example and help the new contributor to learn how to properly ask a question? – ReynierPM Nov 14 '19 at 19:30
  • What "example"? The bottom of the question already includes the code that OP is using. What you are showing is no different. And asking for question clarification (if you wanted to know the PHP version) is what comments are for. N.B.: not my DV – Patrick Q Nov 14 '19 at 19:33