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.

2
  • 1
    Is this question on duplicates a duplicate? stackoverflow.com/questions/6238092/… Commented Nov 13, 2014 at 0:13
  • I think it is very similar, but this question is clearer because it doesn't include any other stuff (like the XML). Commented Nov 13, 2014 at 0:18

2 Answers 2

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

$result = array_map('array_unique', $data);
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

+1 Because it is a completely viable option and works!

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.