1

I have a multidimensional array such as

Array
(
    [0] => Array
        (
            [id] => 1355698
            [comment] => hello
        )

    [1] => Array
        (
            [id] => 1355699
            [comment] => hey
        )

    [2] => Array
        (
            [id] => 1355700
            [comment] => hello
        )

)

The id will always be unique, so the uniqueness of the element will be based on the subarray's value of comment. In this case, array[0] has a duplicate array[2]. (I wonder if there is a better way to phrase this explaination).

array_unique() will not work in this case. Is there an alternative? Thanks!

3

2 Answers 2

2

Create a new array with id and comment as key=>value pairs, then array_unique().

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

2 Comments

like: array( 1344698 => "hello", 1355699, => "hey", 1355700 => "hello");
If you wind up with this solution, you'll also have a convenient way to access the comments without iterating.
0
$uniqueArray = array();
foreach ($nonUniqueArray as $i) {
    $uniqueArray[$i['comment']] = $i;
}

// optionally:
// $uniqueArray = array_values($uniqueArray);

var_dump($uniqueArray);

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.