3

Given

[0] => Array
    (
        [0] => ask.com
        [1] => 2320476
    )

[1] => Array
    (
        [0] => amazon.com
        [1] => 1834593
    )

[2] => Array
    (
        [0] => ask.com
        [1] => 1127456
    )

I need to remove duplicate values solely based on first value, regardless of what any other subsequent values may be. Notice [0][1] differs from [2][1] yet I consider this as a duplicate because there are two matching first values. The other data is irrelevant and shouldn't be considered in comparison.

3
  • No, it's not quite the same...notice [0][1] differs from [2][1]. This is not an exact match therefore it will not be removed. I will update the question to specify this. Commented Dec 17, 2012 at 21:45
  • Only $newList = array_map("unserialize", array_unique(array_map("serialize", $newList))); however this is only for comparing exact matches. It doesn't work in my case. Commented Dec 17, 2012 at 21:50
  • Have you made an attempt to solve this yourself? Look at the other solutions; try mould your own and post it asking for help :) Commented Dec 17, 2012 at 21:50

1 Answer 1

4

Try this, assuming that $mainArray is the array you have.

$outputArray = array(); // The results will be loaded into this array.
$keysArray = array(); // The list of keys will be added here.
foreach ($mainArray as $innerArray) { // Iterate through your array.
    if (!in_array($innerArray[0], $keysArray)) { // Check to see if this is a key that's already been used before.
        $keysArray[] = $innerArray[0]; // If the key hasn't been used before, add it into the list of keys.
        $outputArray[] = $innerArray; // Add the inner array into the output.
    }
}
print_r($outputArray);
Sign up to request clarification or add additional context in comments.

3 Comments

This appears to work how I need it to...obviously not knowing a ton of comparison methods, is this as clean as it gets?
Oh and I would give another +1 for the commenting :) Thank you!
what about if you have three columns for each row?

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.