hakre
hakre

Reputation: 197659

How to get the duplicate keys of multiple arrays?

I know about array_intersect_key which returns duplicate keys of the first parameter in any of the following parameters.

However I was wondering which would be the easiest way to find duplicate keys across more than two arrays at once? Does PHP offer such a function or do I need to do it with multiple calls?

Given

$a1 = array('hello'=>1, 'tag'=>1);
$a2 = array('moin'=>1);
$a3 = array('moin'=>1, 'tag'=>1);

Duplicate keys across all three arrays are moin and tag.

I thought so far about calling array_intersect_keys on each possible pair of parameters (in a function accepting a 2-n number of arrays as parameters) but have problems to actually find all possible combinations. And perhaps there is a much more easier way to do this.

Upvotes: 1

Views: 1234

Answers (5)

Steffen
Steffen

Reputation: 2237

I think your idea is good - call it on every possible combination of arrays. Two nested for loops should be enough to get all combinations:

function array_duplicate_keys() {
    $arrays = func_get_args();
    $count = count($arrays);
    $dupes = array();
    // Stick all your arrays in $arrays first, then:
    for ($i = 0; $i < $count; $i++) {
        for ($j = $i+1; $j < $count; $j++) {
            $dupes += array_intersect_key($arrays[$i], $arrays[$j]);
        }
    }
    return array_keys($dupes);
}

Upvotes: 2

Yoshi
Yoshi

Reputation: 54649

Ugly, but does the job:

function array_duplicate_keys() {
    return array_keys(array_filter(array_count_values(call_user_func_array('array_merge', array_map('array_keys', func_get_args()))), function ($num) {
        return $num > 1;
    }));
}

$a1 = array('hello'=>1, 'tag'=>1);
$a2 = array('moin'=>1);
$a3 = array('moin'=>1, 'tag'=>1);

print_r(
    array_duplicate_keys($a1, $a2, $a3)
);

Output:

Array
(
    [0] => tag
    [1] => moin
)

Upvotes: 1

anubhava
anubhava

Reputation: 785008

All you need is a simple call to array_merge function:

$a = array_merge($a1, $a2, $a3);
print_r($a);

OUTPUT

Array
(
    [hello] => 1
    [tag] => 1
    [moin] => 1
)

Upvotes: 1

Dogbert
Dogbert

Reputation: 222108

$a1 = array('hello'=>1, 'tag'=>1);
$a2 = array('moin'=>1);
$a3 = array('moin'=>1, 'tag'=>1);

$all = array($a1, $a2, $a3);

function pair_duplicate_keys($arrays) {
    $keys = array();
    $result = array();
    foreach ($arrays as $array) {
        foreach ($array as $key => $value) {
            if (!isset($keys[$key])) {
                $keys[$key] = 1;
            } else {
                $result[$key] = 1;
            }
        }
    }
    return array_keys($result);
}


print_r(pair_duplicate_keys($all));

Output

Array
(
    [0] => moin
    [1] => tag
)

Upvotes: 1

user142162
user142162

Reputation:

Here's a custom function I made which does what you're looking for:

function array_duplicate_keys()
{
    $keys = array();
    foreach(func_get_args() as $arr)
    {
        if(!is_array($arr))
        {
            continue;
        }
        foreach($arr as $key => $v)
        {
            if(!isset($keys[$key]))
            {
                $keys[$key] = -1;
            }
            $keys[$key]++;
        }
    }
    return array_keys(array_filter($keys));
}

$a1 = array('hello'=>1, 'tag'=>1);
$a2 = array('moin'=>1);
$a3 = array('moin'=>1, 'tag'=>1);
/*    
print_r(array_duplicate_keys($a1, $a2, $a3));
Array
(
    [0] => tag
    [1] => moin
)
*/

Upvotes: 3

Related Questions