0

I need help. Want to remove duplicate values from array in php.

$categoryTotal echoes 144 125 262 108 351 177 266 269 270 268 309 144 125 262 238 108

On using:

vardump() 

I get

Array ( [0] => 144 [1] => 125 [2] => 262 [3] => 108 [4] => 351 [5] => 177 [6] => 266 [7] => 269 [8] => 270 [9] => 268 [10] => 309 [14] => 238 ) 

I used sort to sort the values in ascending order I get:

108 108 125 125 144 144 177 238 262 262 266 268 269 270 309 351 

but then using $categoryTotal=array_unique($categoryTotal,SORT_NUMERIC); few values like 309 351 disappear.

Would like to know how to know out duplicate values from this array.

2
  • can you put the entire code, it's a little bit confusing here Commented Apr 17, 2013 at 15:09
  • Are those disappearing values integers, too, or are they strings... when mixing strings, ints and floats you might get unexpected results, which is stated in the docs Commented Apr 17, 2013 at 15:12

2 Answers 2

3

Asked before.

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks all. Issue was i was running for ($i=0 to $i<count) rather than using foreach.Because of the same output of array was skipping few values. By using for(range) instead of for each the loop used to break when the count or the range value matched the key of array. Hope you understand. Thanks
1

Use the array_unique() function, like this:

$noDuplicates = array_unique($categoryTotal);

Docs here.

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.