Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with alphabetic sorting of array value

LEGEND ,
Jun 27, 2008 Jun 27, 2008
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
TOPICS
Server side applications
234
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 27, 2008 Jun 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 27, 2008 Jun 27, 2008
LATEST
Thanks David. Works great.

Best,

Brett
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines