0

I want to get rid of duplicates in my array but I'm still printing duplicates with this:

$getuser = ltrim($top10['url'], " users/" );  //trim url to get user id
$array[$i++] = $getuser; 
$dirty = $array;
$clean = array_unique($dirty);
print_r($clean)."<br />";

Input print_r($array)

Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )

Output print_r($clean);

Array ( [0] => 33 [1] => 3 [2] => 29 [3] => 3104 ) Array ( [0] => 156686 [1] => 5 [2] => 3104 [3] => 1 ) Array ( [0] => 2 [1] => 115023 [2] => 185367 [3] => 180694 ) Array ( [0] => 2 [1] => 5 [2] => 3104 [3] => 139403 ) Array ( [0] => 3110 [1] => 2723 [2] => 8087 [3] => 97410 ) Array ( [0] => 1925 [1] => 60 [2] => 18995 [3] => 2940 ) Array ( [0] => 103205 [1] => 111503 [2] => 2 [3] => 128715 ) Array ( [0] => 3 [1] => 119266 [2] => 4 [3] => 3104 ) Array ( [0] => 32565 [1] => 2743 [2] => 148584 [3] => 3505 ) Array ( [0] => 35282 [1] => 99136 [2] => 54167 [3] => 5326 )
7
  • The second parameter to ltrim doesn't do what I think you intend it to do. It is interpreted as a charlist, not a complete string. Apr 24, 2010 at 18:21
  • I've tried $getuser = substr($top10['url'],7) but it leaves 7 null chars. The trim() function I use works fine. My problem is that I am still getting duplicates.
    – patrick
    Apr 24, 2010 at 18:28
  • Can you post an example of your input and output? If you have null characters happening, they are probably interfering with the comparisons that array_unique is doing to determine if two values are duplicates.
    – zombat
    Apr 24, 2010 at 18:53
  • By using trim() I have no more null characters. The string sizes are the same when I do a var_dump.
    – patrick
    Apr 24, 2010 at 20:52
  • Your code looks like it is being executed in a loop, given the counter. Are you showing us multiple, separate inputs and outputs, or is that the input/output in one go? In any case, there are no duplicates in any of the output arrays. I think you may have a logical error.
    – erisco
    Apr 24, 2010 at 21:26

1 Answer 1

0

Yes, it seems to be correct. Basically you specify an array to array_unique and it gives you unique items out of the array.

$unique_array = array_unique($your_array);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.