Page 1 of 1

[SOLVED]question about an array

Posted: Fri Mar 10, 2006 2:28 pm
by rubberjohn
I thought my earlier post had been solved but i dont think i explained it very well.

I have found what the problem is

say u have an array with 10 values in it but half of them are duplicates - (so you only actually have 5 unique values) if you then perform array_unique() on the array you will discard the duplicate values.

my question is what is the best way to reset the indexing of the array (the association of the index and the value is not important)?

As you can see from the loop below the new (unique) array will only contain 5 values and as i am using the count() of the array for the loop it will only loop 5 times. As i am using the loop variable $i to step through the array, any values with an index of greater than 5 will be missed.

Does that make sense?

Code: Select all

$arr_unique_saved_tags = array_unique($arr_saved_tags);


$array_size=count($arr_unique_saved_tags);


$related_tags =  '<select name="related_tags" SELECTED>Choose a tag';
$related_tags .= '<option value="" selected="selected">Make a Selection</option>';

for($i=0; $i<=$array_size; $i++){

$related_tags .= '<option value="' . $arr_unique_saved_tags[$i] . '">' . $arr_unique_saved_tags[$i] . '</option>';
}

$related_tags .=  '</select>';
thanks in advance for your help

rj

Posted: Fri Mar 10, 2006 2:33 pm
by feyd
array_values()

although you could just foreach through the array..

Posted: Fri Mar 10, 2006 2:39 pm
by chrys
I th ink sort($array) would do it too, one of the sorts re-indexes...

Posted: Fri Mar 10, 2006 2:45 pm
by feyd
most of the sort functions reindex, but does rubberjohn want to rearrange the values too?

Posted: Fri Mar 10, 2006 2:49 pm
by rubberjohn
cheers guys

Posted: Fri Mar 10, 2006 2:49 pm
by AKA Panama Jack
Or try this...

Code: Select all

$arr_unique_saved_tags = array_unique($arr_saved_tags); 

$related_tags =  '<select name="related_tags" SELECTED>Choose a tag'; 
$related_tags .= '<option value="" selected="selected">Make a Selection</option>'; 

foreach($arr_unique_saved_tags as $value)
{
	$related_tags .= '<option value="' . $value . '">' . $value . '</option>'; 
}

$related_tags .=  '</select>';

Posted: Fri Mar 10, 2006 2:58 pm
by rubberjohn
sorry closed that prematurley...
most of the sort functions reindex, but does rubberjohn want to rearrange the values too?
basically all i wanted was to get rid of the gaps that were present in the resulting drop down box.

and thanks AKA Panama Jack i was just finishing off a foreach after feyds suggestion

anyway cheers again guys

rj