3

Is there a way to use Array_unique function working for nested array like below? I want to get rid of date duplicates and get two dates out as an array...

Array
(
    [0] => Array
        (
            [value] => 1311044688
            [name] => 19th Jul 2011
        )

    [1] => Array
        (
            [value] => 1311044919
            [name] => 19th Jul 2011
        )

    [2] => Array
        (
            [value] => 1311045076
            [name] => 19th Jul 2011
        )

    [3] => Array
        (
            [value] => 1311164873
            [name] => 20th Jul 2011
        )

    [4] => Array
        (
            [value] => 1311165000
            [name] => 20th Jul 2011
        )

)
3
  • All you want is an array of unique dates? Commented Jul 20, 2011 at 18:52
  • Can you post the result you want. I do not get how you want to remove one of the duplicate... how will you know which of the two to remove? Commented Jul 20, 2011 at 18:53
  • Yes, I want unique dates only. Commented Jul 20, 2011 at 18:55

2 Answers 2

5

I'd write this array to another array using the date as the keys, and the "values" as the value. That's probably the fastest way to achieve what you're looking for.

Something like:

$uniqueAry = array()
foreach ($ary as $item) {
  $uniqueAry[$item['name']] = $item['value'];
}

You'd probably want to include a bit of logic to determine which value takes precedence in case of dupes.

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

1 Comment

Yes, this is the fastest way. 0(n) complexity.
0

You could build up an array of dates then use array_unique on the date array.

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.