...

View Full Version : Sorting Array Alphabetically For Select Box



Pie
01-18-2010, 10:44 AM
Hey guys,

I have some array code which I have put into a select box. I am trying to sort the array so it's alphabetical (not by key), but I don't seem to be able to figure it out. Here's my code:

<select name="ethnicity" id="ethnicity" class="">
<option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
<?php
foreach (array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian") as $v)
echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
?>
</select>

It's usually easy to use sort(); but in this case I'm a little confused as to how I'd sort this without errors. Could you guys show me how you'd do it?

Thanks. :thumbsup:
Pie

abduraooft
01-18-2010, 10:49 AM
It's usually easy to use sort(); but in this case I'm a little confused as to how I'd sort this without errors. Could you guys show me how you'd do it?

<?php
$options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
$options=sort($options);
foreach ( $options as $v)
echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
?> ?

Pie
01-18-2010, 11:09 AM
Thanks abduraooft I can see that's a better way of doing it but perhaps I'm doing something wrong as it does not work....this is what I am trying....

<select name="ethnicity" id="ethnicity" class="">
<option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
<?php
$options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
$options=sort($options);
foreach ( $options as $v)
echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
?>
</select>

Thanks.

Pie
01-18-2010, 01:03 PM
Anyone?

Fou-Lu
01-18-2010, 01:50 PM
Sort takes a reference to an array and returns a boolean to indicate a success. In other words, it sorts an array in place and does not return a sorted array:


$options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
sort($options);
foreach ( $options as $v)

JAY6390
01-18-2010, 02:05 PM
Yeah assigning it back to $options was the slip up. Fou-Lu's solution will work fine

Pie
01-18-2010, 02:32 PM
Thanks Fou-Lu but I don't understand that code, how would that fit with my code???:

<select name="ethnicity" id="ethnicity" class="">
<option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
<?php
$options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
$options=sort($options);
foreach ( $options as $v)
echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
?>
</select>

JAY6390
01-18-2010, 02:36 PM
<select name="ethnicity" id="ethnicity" class="">
<option<?php echo empty($ethnicity)?' selected="selected"':''?> value="" >Choose</option>
<?php
$options=array("Scandinavian","Aboriginal","Latin American","Maori","Hispanic","Indian");
sort($options);
foreach ( $options as $v)
echo "<option".($ethnicity==$v?' selected="selected"':'')." value=\"".htmlspecialchars($v)."\">".htmlspecialchars($v)."</option>\n";
?>
</select>

abduraooft
01-18-2010, 02:51 PM
Sort takes a reference to an array and returns a boolean to indicate a success. In other words, it sorts an array in place and does not return a sorted array:
Ah.. my bad :o

Pie
01-18-2010, 02:59 PM
That's the one Jay. Thanks everyone.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum