[SOLVED]question about an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

[SOLVED]question about an array

Post 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
Last edited by rubberjohn on Fri Mar 10, 2006 3:23 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_values()

although you could just foreach through the array..
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

I th ink sort($array) would do it too, one of the sorts re-indexes...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

most of the sort functions reindex, but does rubberjohn want to rearrange the values too?
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

cheers guys
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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>';
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post 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
Post Reply