0

This is my array output -

print_r($array_email);

    Array
(
    [1] => Array
        (
            [0] => [email protected]
            [1] => [email protected]
            [2] => [email protected]
            [3] => [email protected]
            [4] => [email protected]
        )

    [2] => Array
        (
            [0] => [email protected]
        )

    [3] => Array
        (
            [0] => [email protected]
        )

    [4] => Array
        (
            [0] => [email protected]
        )

)

Here i have tried - (but not working)

function get_duplicates( $array ) {
                return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
            }

print_r(get_duplicates($array_email));

I need output like that-

Array
    (
        [1] => Array
            (
                [1] => [email protected]
                [2] => [email protected]
                [4] => [email protected]
            )

        [2] => Array
            (
                [0] => [email protected]
            )
        [4] => Array
            (
                [0] => [email protected]
            )

    )

2 Answers 2

3

If I understood your requirement correctly:

array_map(function($elem) {
    return array_unique($elem);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please explain me how to use it ?
I may be completely misunderstanding the question, and I've certainly got no idea how the 2 arrays in the example given actually relate to each other... but doesn't this give the exact opposite of "Find duplicates" in that it removes them rather than finds them?
@vikastyagi This call returns the filtered array.
0

You have to do this:

array_walk($arr, function(&$value) {
    $value = array_unique($value);
});

$arr = array_unique($arr, SORT_REGULAR);

array_walk will remove duplicates inside inner arrays. array_unique will do the rest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.