1

I want to remove duplicate values from an array.

Here is my array

$arr=array([0]=>123,[1]=>223,[2]=>323,[3]=>123,[4]=>223);

For removing duplicate values, I used array_unique() function, but it still shows the same array.

Is there any method to solve this problem?

2
  • You probably used array_unique() but did not assign its result to an array variable. Look at how @diEcho is assigning the result to $result. Commented May 11, 2011 at 6:41
  • Now the question is... did you assing the array_unique to a variable ? i.e $result = array_unique($arr); Commented May 11, 2011 at 6:41

3 Answers 3

2

use array_unique()

<?php
$arr=array([0]=>123,[1]=>223,[2]=>323,[3]=>123,[4]=>223);
$result = array_unique($arr);
print_r($result);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@ oscar sorry it was mistake , i was suppose to post that in other,thanx for pointing it,
1

Your code works fine for me.

$arr = array(0 => 123, 1 => 223, 2 => 323, 3 => 123, 4 => 223);

var_dump(array_unique($arr));

Output

array(3) {
  [0]=>
  int(123)
  [1]=>
  int(223)
  [2]=>
  int(323)
}

CodePad.

Note that array_unique() returns a new array, it doesn't take the array by reference, so you'll need to assign the returned array somewhere.

1 Comment

array_merge() and array_filter(), here am used to merge number of array and remove null values of array, after that i had used array_unique() to avoid duplicate values,but it gives the same array
0

try this

$arrUnique = array_unique($arr);
print_r($arrUnique);

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.