Forum Moderators: coopster

Message Too Old, No Replies

resetting keys w/ array unique

         

sssweb

7:51 pm on Dec 31, 2007 (gmt 0)

10+ Year Member



How do I reset the array keys to basic 0,1,2,3,etc. after using array_unique()? That function preserves the original keys, so when I loop through it, the count is messed up:

$array = array(0=>'a', 1=>'b', 2=>'b', 3=>'c');
$array = array_unique($array);

// $array now = array(0=>'a', 1=>'b', 3=>'c')
// count($array) = 3

for($i = 0; $i < count($array); $i++)
{
// can't fully iterate through loop because $i doesn't correspond to keys, some of which no longer exist
}

Thought re-setting keys would be a simple function, but I don't see it in the manual.

eelixduppy

8:01 pm on Dec 31, 2007 (gmt 0)



Try something like this:

$unique_array = array();
$unique_array = [url=http://www.php.net/array-values]array_values[/url](array_unique($array));

sssweb

8:51 pm on Dec 31, 2007 (gmt 0)

10+ Year Member



Thanks, that works.

Seems like there ought to be an optional setting in array_unique() to reset them....