Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts

    php array_unique question

    I've been working on a troublesome piece of code for hours now, and I've finally realised that the problem is that when array_unique removes duplicates from an array, the ones that don't get removed mash up the sequence of the array, IE $array[0]="something";
    $array[13]="something else";
    $array[15]="not duplicate";

    how do I put them back in sequence, IE
    $array[0]="something";
    $array[1]="something else";
    $array[2]="not duplicate";

    any help would be much appreciated!
    thanks, Gus

  • #2
    New to the CF scene
    Join Date
    Aug 2006
    Location
    Germany, Heilbronn
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    You can iterate your unsorted array like this:

    Code:
    >> $array = array();
    array (
    )
    >> $array[0] = "something";
    'something'
    >> $array[13] = "something else";
    'something else'
    >> $array[15] = "no duplicate";
    'no duplicate'
    >> $array;
    array (
      0 => 'something',
      13 => 'something else',
      15 => 'no duplicate',
    )
    >> $tmp = array();
    array (
    )
    >> foreach ($array as $e) { $tmp[] = $e; };
    
    >> $tmp
    array (
      0 => 'something',
      1 => 'something else',
      2 => 'no duplicate',
    )
    >> $array = $tmp;
    array (
      0 => 'something',
      1 => 'something else',
      2 => 'no duplicate',
    )
    >> unset($tmp);
    
    >>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •