This is a discussion on "Sorting a PHP array" within the PHP Forum section. This forum, and the thread "Sorting a PHP array are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Sorting a PHP array
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
Sorting a PHP array
I have an array of items from a PHP session that I want to display in a HTML table along with the value for each item. Because in the same table I'm going to be pulling data from a db too, I need the items in the array and their values to always be pulled out and displayed in the same order. The problem is that they might not be added to the array in the same order.
For example, print_r($_SESSION['objects']); five minutes ago gave me Array ( [apples] => 2 [pears] => 2 [bananas] => 1 [kiwi] => 3) The next time, Array ( [pears] => 2 [bananas] => 2 [apples] => 1 [kiwi] => 3) What I need is for them to always be displayed in the same order in the table, say alphabetically, so in the table I'd have, Fruit | Number | apples | 2 | bananas | 3 | kiwi | 1 | pears | 2 | Ordinarily I'd probably use a foreach loop to write out the rows, but the pulling stuff from the db at the same time's complicating things, so I'm using boring static table markup and trying to put the results of the array in as variables. Any idea how I'd do this? |
|
|
|
||||
|
Re: Sorting a PHP array
Hrmmm...
Can't you just use
Last Blog Entry: Happy Holidays - A Non Offensive way to say Merry Christmas? WRONG! (Dec 11th, 2007)
|
|
|||
|
Re: Sorting a PHP array
Thanks for the suggestion. I've just tried asort and it doesn't seem to work for me though. ksort does though, and I've now got it set up using foreach again - I'm not sure quite what the difference between asort and ksort is. At least that's working though. Now all I've got to do is to figure out how to write the result of the mysql query into the third column at the same time.
|
|
||||
|
Re: Sorting a PHP array
Last Blog Entry: Happy Holidays - A Non Offensive way to say Merry Christmas? WRONG! (Dec 11th, 2007)
|
![]() |
| Tags |
| array, arrays, php, sort, sorting |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help sorting an array | djeyewater | PHP Forum | 6 | Apr 18th, 2008 15:16 |
| [SOLVED] Array sorting | welshstew | Classic ASP | 6 | Nov 28th, 2007 16:45 |
| Sorting a new array from an existing array | Ozeona | Flash & Multimedia Forum | 2 | Sep 20th, 2005 08:43 |
| array unable to check another array so as to be displayed | Ozeona | Flash & Multimedia Forum | 1 | Aug 5th, 2005 10:26 |
| Sorting the array - reversing the sequence codes? | Ozeona | Flash & Multimedia Forum | 1 | Aug 3rd, 2005 01:52 |