Sorting a PHP array

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Apr 20th, 2007, 20:00
Junior Member
Join Date: May 2006
Location: UK
Age: 35
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Reply With Quote

  #2 (permalink)  
Old Apr 20th, 2007, 20:21
JustinStudios's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: USA
Posts: 406
Blog Entries: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JustinStudios
Re: Sorting a PHP array

Hrmmm...
Can't you just use
Code: Select all
asort($my_array);
That'd keep the text applying in the same order.
Reply With Quote
  #3 (permalink)  
Old Apr 20th, 2007, 22:13
Junior Member
Join Date: May 2006
Location: UK
Age: 35
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Reply With Quote
  #4 (permalink)  
Old Apr 20th, 2007, 22:18
JustinStudios's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: USA
Posts: 406
Blog Entries: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JustinStudios
Re: Sorting a PHP array

Code: Select all
ksort — Sort an array by key 
asort — Sort an array and maintain index association
I may have misread what you were wanting. At least it is working now .
Reply With Quote
Reply

Tags
array, arrays, php, sort, sorting

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 21:01.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

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 34 35 36 37 38 39 40 41 42 43