1

I have an array like this

Array
(
    [0] => u1,u2
    [1] => u2,u1
    [2] => u4,u3
    [3] => u1,u3
    [4] => u1,u2
)

I want to remove similar values from the array I want an out put like

Array
    (
        [0] => u1,u2
        [1] => u4,u3
        [2] => u1,u3

    )

I tried to loop thru the input array, sort the value of the indexes alphabetically and then tried array_search to find the repeated values. but never really got the desired output

any help apprecated

4
  • php.net/manual/en/function.array-unique.php Commented Aug 27, 2013 at 10:36
  • @RoyalBg: since array_unique was not working i tried all these steps.It will not work Commented Aug 27, 2013 at 10:40
  • As I have said in the comments to the answers, none of them will work since u1,u2 !== u2,u1. array_unique matches exact values only. Commented Aug 27, 2013 at 10:40
  • Tried exploding the pairs in another arrays and compare the unique? Commented Aug 27, 2013 at 10:41

5 Answers 5

3

You cannot use array_unique() alone, since this will only match exact duplicates only. As a result, you'll have to loop over and check each permutation of that value.

You can use array_unique() to begin with, and then loop over:

$myArray = array('u1,u2', 'u2,u1', 'u4,u3', 'u1,u3', 'u1,u2');

$newArr = array_unique($myArray);
$holderArr = array();

foreach($newArr as $val)
{
    $parts = explode(',', $val);
    $part1 = $parts[0].','.$parts[1];
    $part2 = $parts[1].','.$parts[0];

    if(!in_array($part1, $holderArr) && !in_array($part2, $holderArr))
    {
        $holderArr[] = $val;
    }
}

$newArr = $holderArr;

The above code will produce the following output:

Array ( 
    [0] => u1,u2 
    [1] => u4,u3 
    [2] => u1,u3 
)
Sign up to request clarification or add additional context in comments.

3 Comments

This solves my problem. Thanks a lot Ben :). Thanks to all who tried to solve this.
Thought it was the decision :) +1 for the whole answer.
Glad to help. A note to the others, it sometimes helps to read the whole question :)
0

Use array_unique() PHP function:

http://php.net/manual/en/function.array-unique.php

3 Comments

I don't think array_unique will work here, since u1,u2 !== u2,u1.
" want to remove similar values" u1,u2 !== u2,u1 -> they are not similar i think.
See the desired output. unique != similar.
0

Use the function array_unique($array)

Comments

0
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

php manual

1 Comment

I don't think array_unique will work here, since u1,u2 !== u2,u1.
0

since u1,u2 !== u2,u1

    $array=array('u1,u2','u2,u1','u4,u3','u1,u3','u1,u2');
    foreach($array as $k=>$v)
    {
        $sub_arr = explode(',',$v);
        asort($sub_arr);
        $array[$k] = implode(',',$sub_arr);
    }

    $unique_array = array_unique($array);

    //$unique_array = array_values($unique_array) //if you want to preserve the ordered keys

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.