| |||||||||||
| |||||||||||
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:
(written 2006-02-13 00:39:38) Associated topics are indexed under H106 - PHP - ArraysH999 - Additional PHP Material H112 - PHP - Further Web Page and Network Handling
Some other Articles
On being BritishTrain 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 pageThis 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). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||