0

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.

Hard Fitness
  • 452
  • 3
  • 9
  • 24
  • 1
    Is this question on duplicates a duplicate? https://stackoverflow.com/questions/6238092/remove-duplicates-from-array – Stacker-flow Nov 13 '14 at 00:13
  • I think it is very similar, but this question is clearer because it doesn't include any other stuff (like the XML). – JamesG Nov 13 '14 at 00:18

2 Answers2

3
$data = [
    'animals' => ['horse', 'dog', 'dog'],
    'team' => ['cubs', 'reds', 'cubs']
];

$result = array_map('array_unique', $data);
print_r($result);
alu
  • 456
  • 2
  • 7
2

Here's one option:

    $ar = array( 'animals' => array( 'horse', 'dog', 'dog' ),
                 'team' => array( 'cubs', 'reds', 'cubs' ));


    foreach( $ar as &$item )
    {
        $item = array_unique( $item );
    }

    print_r( $ar );

Not as cool as using array_map(), but it works.

JamesG
  • 4,288
  • 2
  • 31
  • 36