Jump to content

Dynamically Combine Arrays With Array_Intersect()


cyberRobot

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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; }
}
?>

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.