Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
PHP - setting sort order with an associative array
I was updating one of our web site scripts a few hours back, and I wanted to sort the rows of an HTML table generated by the PHP from a directory listing. Problem was that I was making the table up using concatanation (.=) operators in a single string.

First solution - put all the table rows into an array and sort the array. Alas, this solution was ugly because the usort routine needed to sort the list some ugly code to unpick the table rows that I had just carefully created.

Second solution - and very neat though I say it myself - was to put the table rows into an associative array and set up the keys of the array to be the sort key. All I need to then is a ksort to get the rows in order, and an implode to join up the values in the table since implode ignores the keys if its input is associative

Code:


$fill = array();
$dh = opendir($source);
while ($name = readdir($dh)) {
/* code to work out the table row ... */
$fill[$name] = /* Nasty per-line code! */
}
ksort($fill);
$fillit = implode("",$fill);

(written 2006-02-13 00:39:38)

 
Associated topics are indexed under
H106 - PHP - Arrays
H999 - Additional PHP Material
H112 - PHP - Further Web Page and Network Handling

Back to
MP face to face
Previous and next
or
Horse's mouth home
Forward to
Perl - multiprocess applications

Some other Articles
On being British
Train service from December
Design your day with a walk
Perl - multiprocess applications
PHP - setting sort order with an associative array
MP face to face
A fond memory of Sir Freddie
It costs nothing to say THANK YOU
Perl/Tk real time display
Should we cruise around the world?
1638 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho