Skip to main content
Inspiring
June 27, 2008
Question

Help with alphabetic sorting of array value

  • June 27, 2008
  • 2 replies
  • 232 views
Hi All,

I've created a form with a drop down list populated from a text file
array. I want to alphabetically sort the options based on the array
value but I don't know how to do that. Below is the code, any help
would be appreciated.

> <?php
> $textfile = 'car_makes.txt';
> if (file_exists($textfile) && is_readable($textfile)) {
> // read the file into an array named $namearray
> $namearray = file($textfile);
> }
> else
> {
> echo "Can't open $textfile";
> }
> ?>
>
> <form id="form1" name="form1" method="post" action="">
> <select name="choice" id="choice">
> <option value="">Select your choice</option>
> <?php
> foreach ($namearray as $name)
> {
> echo "<option value='$name'>$name</option>\n";
> }
> ?>
> </select>
> </form>

TIA

Brett
This topic has been closed for replies.

2 replies

Inspiring
June 27, 2008
Thanks David. Works great.

Best,

Brett
Inspiring
June 27, 2008
Brett wrote:
> I've created a form with a drop down list populated from a text file
> array. I want to alphabetically sort the options based on the array
> value but I don't know how to do that.

Try looking in the PHP manual. There's a function that does precisely
what you want:

http://docs.php.net/manual/en/function.sort.php

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/