Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersecting multidimensional array of varying size

I have a multidimensional array that looks like this:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

        [1] => Array
            (
                [id] => 1
            )

        [2] => Array
            (
                [id] => 2
            )

        [3] => Array
            (
                [id] => 5
            )

        [4] => Array
            (
                [id] => 4
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 1
            )

        [1] => Array
            (
                [id] => 3
            )

        [2] => Array
            (
                [id] => 4
            )

        [3] => Array
            (
                [id] => 5
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

    )

)

I need to find a way to return the intersecting value(s). In this case that would be

[id] => 3

The length of the array could be different, so I can't just use array_intersect().

like image 384
Jakob Løkke Madsen Avatar asked Jan 07 '10 13:01

Jakob Løkke Madsen


3 Answers

This would be simple if your arrays contained only integers, but as they contain an another array, it gets a bit more complicated. But this should do it:

function custom_intersect($arrays) {
    $comp = array_shift($arrays);
    $values = array();

    // The other arrays are compared to the first array:
    // Get all the values from the first array for comparison
    foreach($comp as $k => $v) {
        // Set amount of matches for value to 1.
        $values[$v['id']] = 1;
    }

    // Loop through the other arrays
    foreach($arrays as $array) {
        // Loop through every value in array
        foreach($array as $k => $v) {
            // If the current ID exists in the compare array
            if(isset($values[$v['id']])) {
                // Increase the amount of matches
                $values[$v['id']]++;
            }
        }
    }

    $result = array();

    // The amount of matches for certain value must be
    // equal to the number of arrays passed, that's how
    // we know the value is present in all arrays.
    $n = count($arrays) + 1;
    foreach($values as $k => $v) {
        if($v == $n) {
            // The value was found in all arrays,
            // thus it's in the intersection
            $result[] = $v;
        }
    }
    return $result;
}

Usage:

$arrays = array(
    array(array('id' => 3), array('id' => 1), array('id' => 2), array('id' => 5), array('id' => 4)),
    array(array('id' => 1), array('id' => 3), array('id' => 4), array('id' => 5)),
    array(array('id' => 3))
);

print_r(custom_intersect($arrays));

Result:

Array
(
    [0] => 3
)

This function isn't perfect: if you have duplicate ID's in one array, it will not work. That would require a bit more code to first make the array values unique, but this will probably work in your case.

like image 193
Tatu Ulmanen Avatar answered Oct 08 '22 07:10

Tatu Ulmanen


You can use array_uintersect() to get the intersection of arrays using a custom comparison function. You have to call it with call_user_func_array() though, as it expects each array as a separate argument:

//build list of parameters for array_uintersect()
$params = array_merge($input, array('compare_func'));

$result = call_user_func_array('array_uintersect', $params);

function compare_func($a, $b) {
    return $a['id'] - $b['id'];   
}

You can't simply call array_intersect() with call_user_func_array(), because it seems to compare arrays by comparing their string representation (which will always be 'Array').

like image 26
Tom Haigh Avatar answered Oct 08 '22 06:10

Tom Haigh


$a = array(4,3);
$b = array(4,3,2,1);
$c = array(1,2,3,4,5);
$d = array(5,4,3);

$array = array($a,$b,$c,$d);

for ($i=0; $i<count($array); $i++){
    if ($i==0){
        $array2 = $array[$i];
    } else {
        $array2 = array_intersect($array2, $array[$i]);
    }
}

print_r($array2);`

Result:

3,4

like image 22
Torta János Avatar answered Oct 08 '22 08:10

Torta János