0

I have an array and would like to remove duplicates. I'm using php. How to remove duplicate rows (by id).
My array looks like:

 Array
    (
        [0] => Array
            (
                [id] => 415
            ) 
        [1] => Array
            (
                [id] => 425
            )
        [2] => Array
            (
                [id] => 425
            )
        [3] => Array
            (
                [id] => 426
            )
     )
1
  • Any reason why you have a multidimensional array? Commented Oct 12, 2014 at 9:01

2 Answers 2

1

Assuming that id is the only element of the array, you can walk through the array using serialize and array_unique, as array_unique by itself doesn't work with multidimensional arrays.

$foo = array_map('unserialize', array_unique(array_map("serialize", $foo)));

If you have other elements, @Ghost's answer is probably better

Sign up to request clarification or add additional context in comments.

Comments

0

This will also flatten the array to a 1-dimensional array

$data = array(
    array(
        'id' => 415,
    ),
    array(
        'id' => 425,
    ),
    array(
        'id' => 425,
    ),
    array(
        'id' => 426,
    ),
);


$data = array_unique(
    array_map('end', $data)
);
var_dump($data);

gives

Array
(
    [0] => 415
    [1] => 425
    [3] => 426
)

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.