Search code examples
phparraysarray-uniquearray-map

removing array duplicates from associative array


So i have:

Array (
      [animals] => Array
        (

            [0] => horse
            [1] => dog
            [2] => dog

        )
      [team] => Array
        (

            [0] => cubs
            [1] => reds
            [2] => cubs

        )
)

Trying to eliminate the repeat ones with animals and same with team.

Tried this but didn't help.

$unique = array_map("unserialize", array_unique(array_map("serialize", $result)));

Seems like it doesn't reach deep inside, don't want either to hard code animals or team.


Solution

  • $data = [
        'animals' => ['horse', 'dog', 'dog'],
        'team' => ['cubs', 'reds', 'cubs']
    ];
    
    $result = array_map('array_unique', $data);
    print_r($result);