Sorting arrays into alphabetical order (PHP)

Hi,

How’s it going?

I wonder if you can help please with the following prob?

I have 500 words currently stored in an array, say $words.

Now I’d like to index these words in alphabeticaal order using php.

I’m aware that they can be easily sorted with the ‘sort functions’ into alphabetical order, however, I’d like to only display the A’s on one page, the B’s on another, then the C’s on another, and so on. But, never more than one letter on any one page.

All pages will have links for every letter of the alphabet so the users can jump to the letter (and therefore the page)they desire. Do you know how to achieve this please, in the most efficient way?

Basically, I’m trying to achieve the same end as normal numeric next page links and indexing, except with letters.

Thanks very much, if you can help!

Regards,

Jason

Select words
from words
where words = ‘A%’
order by words

Then change the A to B to C to D etc. You’d probably do it dynamically but you get the idea.

Chris

Yeah, thanks for sorting out my SQL statemnt (other post!!!), but what about PHP arrays please?

Regards,

Jason

I just assumed you’re filling the array from a DB and you’d change the way you fill your array using that SQL.

Chris

Like aspen said, if you have sorted them when selecting data from DB then you don’t have to sort them again with PHP. But to asnwer your question, there are several function to sort an array like sort(). This is taken from PHP manual :


$fruits = array ("lemon", "orange", "banana", "apple");
sort($fruits);

Then $fruit will be sorted alphabetically. Other related functions are rsort(), usort(), ksort(), asort(), and arsort(). You can see what each function does in PHP manual.

Thank you for your help so far gentlemen.

However, I’m taking records from both a database and a PHP array.

There are instances when the data does not come from a database so I need to be able to sort the array alphabetically.

I’m aware of the sort function (for alphabetical sorting) however, I need to go a stage further than this and categorise the array elements depending upon their letter.

So all the A’s are to be categorised, then all the B’s, then all the C’s e.t.c. Basically, eventually I only want to display one category at a time, so I need to break this array up efficiently.

e.g. data at the moment after sort function performed on array:

Apples
Angels
All
Blink
Blank
Block
Chips
Charter
Challenge

  • and what I need is:
    category 1:
    Apples
    Angels
    All

category 2:
Blink
Blank
Block

category3:
Chips
Charter
Challenge

(note: I relaise this isn’t strictly alphabetical, but hopefully you get the point)

Does this clarify my needs?

Please let me know if it doesn’t, otherwise thank you very much if you can help,

Regards,

Jason

What you need is an array of arrays.

I’m not a big enough php buff to tell you how to do it but what you need is an array of 26 arrays, one for each letter, and then sort that array alphabetically, then sort each individual array alphabetically as well.
How to do this?

$letter = functiontotakeleftmostletterfromstring($input);

Insert $letter into array where arrayname = $letter

Just look up the functions that’ll do the above and things should probably work.

Chris