CarmenH Posted July 27, 2010 Share Posted July 27, 2010 Hello, I have three arrays of integers $arrayA, $arrayB, and $arrayC. $ArrayA is a "master array" meaning the array I need to match the other two to. $arrayB and $arrayC are generated by my functions in the application and meet specific criteria. As such, arrays B and C will never have any intersecting values, but both should have matches in $arrayA, and I need to generate an array which contains only the values of arrayA which match either B or C. After finding some documentation here (http://www.metrocomp.mocsi.eu/w3/php/func_array_intersect.html) I tried using array_intersect : array_intersect ($arrayA, $arrayB, $arrayC) However this does not work as described and returns no results as there are no values which are contained in all three arrays. I wrote the below as a work around, and it works just fine but I would think there is a better way? $temp1 = array_intersect($arrA, $arrB); $temp2 = array_intersect($arrA, $arrC); $temp3 = array_merge($temp1, $temp2); $final_array = array_unique($temp3); Thanks for looking! Carmen Quote Link to comment https://forums.phpfreaks.com/topic/209023-using-array_intersect-with-a-master-array-and-two-sub-arrays/ Share on other sites More sharing options...
Alex Posted July 27, 2010 Share Posted July 27, 2010 That method is fine, but you could clean it up a bit without using all those temporary variables. $arr = array_unique( array_merge( array_intersect($arrA, $arrB), array_intersect($arrA, $arrC) ) ); Quote Link to comment https://forums.phpfreaks.com/topic/209023-using-array_intersect-with-a-master-array-and-two-sub-arrays/#findComment-1091757 Share on other sites More sharing options...
AbraCadaver Posted July 27, 2010 Share Posted July 27, 2010 This as well: $arr = array_intersect($arrA, array_merge($arrB, $arrC)); Quote Link to comment https://forums.phpfreaks.com/topic/209023-using-array_intersect-with-a-master-array-and-two-sub-arrays/#findComment-1091760 Share on other sites More sharing options...
CarmenH Posted July 28, 2010 Author Share Posted July 28, 2010 Those two solutions are great ! I think I'll implement the second one. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/209023-using-array_intersect-with-a-master-array-and-two-sub-arrays/#findComment-1092159 Share on other sites More sharing options...
AbraCadaver Posted July 28, 2010 Share Posted July 28, 2010 Those two solutions are great ! I think I'll implement the second one. Thank you! I just look back at my code. Only use with arrays that are numerically indexed, not with associative arrays. Quote Link to comment https://forums.phpfreaks.com/topic/209023-using-array_intersect-with-a-master-array-and-two-sub-arrays/#findComment-1092250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.