Skip to content

Instantly share code, notes, and snippets.

@ricardoboss
Created May 15, 2022 13:41
Show Gist options
  • Save ricardoboss/0b1129ee32ba7d0d7862fb9806b53c4e to your computer and use it in GitHub Desktop.
Save ricardoboss/0b1129ee32ba7d0d7862fb9806b53c4e to your computer and use it in GitHub Desktop.
<?php
function foo(array ...$as): array
{
assert(count($as) >= 2);
return array_diff_uassoc(array_shift($as), ...$as, fn($a, $b) => $a - $b);
}
@TiiFuchs
Copy link

You could do this:

function foo(array ...$as): array
{
    assert(count($as) >= 2);
    
    return call_user_func_array('array_diff_uassoc', [...$as, fn($a, $b) => $a <=> $b]);
}

@ricardoboss
Copy link
Author

True! Thanks for the suggestion!

@TiiFuchs
Copy link

TiiFuchs commented May 16, 2022

Or call it repeatedly, each time with two arrays from $as. Could even use array_reduce for that:

function foo(array ...$as): array
{
    assert(count($as) >= 2);
    
    return array_reduce(
        array_slice($as, 1, null, true),
        fn($carry, $item) => array_diff_uassoc($carry, $item, fn($a, $b) => $a <=> $b),
        $as[0]
    );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment