cyberRobot Posted October 8, 2012 Share Posted October 8, 2012 I’m using array_intersect() to combine three arrays. $matchingIDs = array_intersect($array1, $array2, $array3); Everything works until one of the arrays is empty. Is there a way to dynamically feed arrays to the function? I could use several if statements and array_intersect() calls, but it seems like there should be a better way. Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/ Share on other sites More sharing options...
kicken Posted October 8, 2012 Share Posted October 8, 2012 look into call_user_func_array Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383714 Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 What do the arrays look like and what do you want to end up with? Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383716 Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 Read the manual for array_intersect: array array_intersect ( array $array1 ' date=' array $array2 [, array $ ... '] )array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved. It doesn't combine arrays, it's used to filter arrays. If you're combining arrays, you want array_merge. What you say you're doing doesn't match the function you're using, yet you say it's working. Answering Barand's question will help us figure out what you really want. Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383727 Share on other sites More sharing options...
cyberRobot Posted October 8, 2012 Author Share Posted October 8, 2012 What do the arrays look like and what do you want to end up with? Here's a quick code sample: <?php $array1 = array(1, 2, 3, 4, 5, 6, 7, ; $array2 = array(2, 3, 5, 7); $array3 = array(1, 2, 3, 7, 9); $matchingIDs = array_intersect($array1, $array2, $array3); print_r($matchingIDs); ?> This gives me a list of IDs which appear in all three arrays: Array ( [1] => 2 [2] => 3 [6] => 7 ) If one of the arrays is empty, however, the result set is also empty. <?php $array1 = array(1, 2, 3, 4, 5, 6, 7, ; $array2 = array(); $array3 = array(1, 2, 3, 7, 9); ?> Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383731 Share on other sites More sharing options...
Jessica Posted October 8, 2012 Share Posted October 8, 2012 Yes. That makes perfect sense. What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383733 Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 Right, you've just described a functioning array_intersect function. Step 2: What do you want instead of what you're doing? It was the part of Barand's question after "and" and before the question mark. Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383735 Share on other sites More sharing options...
cyberRobot Posted October 8, 2012 Author Share Posted October 8, 2012 Yes. That makes perfect sense. What is the problem? If $array1, $array2, $array3 are empty, I want to ignore that array. Note that the reason an array would be empty is because a user chose an option which results in that array containing every ID from the database. Instead of running a query to get everything, I want to ignore that array. Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383737 Share on other sites More sharing options...
Jessica Posted October 8, 2012 Share Posted October 8, 2012 So, write some logic that does that. count. Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383738 Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 (edited) better yet: empty() Edited October 8, 2012 by ManiacDan Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383741 Share on other sites More sharing options...
cyberRobot Posted October 8, 2012 Author Share Posted October 8, 2012 Just to clarify, there isn't a cleaner way of doing this without a bunch of if statements? If so, I may be better off with running the query... For reference, this is basically what I would be looking at: <?php //IF FIRST AND SECOND ARRAY CONTAIN IDS, FIND THE INTERSECT if(!empty($array1) && !empty($array2)) { $matchingIDs = array_intersect($array1, $array2); //ELSE...STORE MATCHES SO FAR } else { if(!empty($array1)) { $matchingIDs = $array1; } elseif(!empty($array2)) { $matchingIDs = $array2; } else { $matchingIDs = array(); } } //IF THE MATCHING IDS SO FAR AND THE THRID ARRAY CONTAIN IDS, FIND THE INTERSECT if(!empty($matchingIDs) && !empty($array3)) { $matchingIDs = array_intersect($matchingIDs, $array3); //ELSE...UPDATE MATCHES } else { if(!empty($array3)) { $matchingIDs = $array3; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383750 Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 so you want the intersection of all 3 arrays, and any one or two of the three may be empty, and therefore you shouldn't use it? $args = array(); if ( !empty($arr1) ) $args[] = &$arr1; if ( !empty($arr2) ) $args[] = &$arr2; if ( !empty($arr3) ) $args[] = &$arr3; if ( count($args) == 1 ) { $new = $args[0]; } else { $new = call_user_func_array('array_intersect', $args); } Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383757 Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 (edited) or if the number of array is variable <?php $a = array ( array(1,2,3,4,5,6,7,, array(2, 3, 5, 7), array() ); $res = array(); foreach ($a as $B) { if (empty($res)) $res = $B; else if (!empty($B)) { $res = array_intersect($res, $B); } } echo '<pre>'.print_r($res, 1).'</pre>'; ?> Edited October 8, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383760 Share on other sites More sharing options...
cyberRobot Posted October 8, 2012 Author Share Posted October 8, 2012 (edited) Thanks for all the responses. I forgot all about looking into kicken suggestion. Thanks for showing me an implementation ManiacDan. I haven't totally wrapped my mind around what's going on, but the code looks much cleaner than mine. Edited October 8, 2012 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/269225-dynamically-combine-arrays-with-array_intersect/#findComment-1383764 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.