Jump to content

Removing duplicates from array doesn't work


utdiscant

Recommended Posts

I made this code to remove all the duplicates from two arrays:

I am putting the values which only exist once into an other array. I only need to check about the $xcoords cause i am just interested in removing duplicated x-coordinates:

$position=0;
for($i=0;$i<sizeof($xcoords_temp);$i++) {
  $found = 0;
  for($n=0;$n<$position;$n++)
    if($xcoords_temp[$i] == $xcoords[$n])
  $found = 1;
  if($found == 0) {
    $xcoords[$position] = $xcoords_temp[$i];
    $ycoords[$position] = $ycoords_temp[$i];
    $position++;
  }
}

If you this code the inputs:
$xcoords_temp = array(1,1,1,2,2,2);
$ycoords_temp = array(2,2,2,1,1,1);

It will generate the output:
1 2
2 1
2 1

Which is wrong because i only want:
1 2
2 1

What is wrong with my code?

David Kofoed Wind
Denmark

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.