rfeio Posted May 13, 2010 Share Posted May 13, 2010 Hi! I have this script that generates a multiple array: array[x][y] Depending on the user input the array can be 'x' can be 1 or more. I want to use the array[x] with the array_intersect as in array_intersect($array[0], $array[1], ...) So I thought of doing: for($k=0; $k < count($array); $k++) { $temp .= '$array['.$k.'],'; } and then issuing: array_intersect($temp) but obviously it doesn't work. So my question is, how can I do this? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/201689-help-with-arrays/ Share on other sites More sharing options...
rfeio Posted May 14, 2010 Author Share Posted May 14, 2010 Just to explain the objective of this exercise: The idea is to check the values that are common to all the arrays. For example let's suppose that: array[1] has the values (a, b, c, d, e) array[2] has the values (a, e, f, g, h) array[3] has the values (e, f, g, h, k) The end result should be 'e' which is common to all the 3 arrays. Now one of the challenges is that I don't know how many arrays I will have in the beginning. In this example it was 3, but it can be more or less then that. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058183 Share on other sites More sharing options...
RichardRotterdam Posted May 14, 2010 Share Posted May 14, 2010 There is prob more then one way to solve this but here is an idea. Why not put all the values in one array and use array_unique to remove the duplicate values. That way you have all possible values stored in 1 array. Then do a check for each possible value if it exists in all the arrays you want to check Quote Link to comment https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058197 Share on other sites More sharing options...
ignace Posted May 14, 2010 Share Posted May 14, 2010 Try Dj Kat's solution or try call_user_func_array Quote Link to comment https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058206 Share on other sites More sharing options...
rfeio Posted May 14, 2010 Author Share Posted May 14, 2010 Yeap, DJ Kat's solution seems a good one. Now, how can I solve my other problem? I don't know how many arrays I have to begin with in order to merge them in one single array. My idea was to create a variable with the arguments for the array_merge() function like: for($i=0; $i < $max_num_arrays; $i++) { $arguments .= '$array['.$i.'],'; } and then: array_merge($arguments) But this doen's work because $arguments is a string and not an array. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058211 Share on other sites More sharing options...
rfeio Posted May 14, 2010 Author Share Posted May 14, 2010 Hummmm... I think I know how to solve this: $merge = array(); for ($i=0; $i < $max_num_arrays; $i++) { $merge = array_merge($merge, $array[$i]); } Quote Link to comment https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058216 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.