...

View Full Version : php array_unique question



gusblake
11-19-2006, 07:57 PM
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

Fu86
11-19-2006, 08:33 PM
You can iterate your unsorted array like this:



>> $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);

>>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum