Sorting array values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Sorting array values

Post by aceconcepts »

Mod | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi,

I have the following array:

Code: Select all

$itemArray[]=array('title' => $_SESSION['stageTitle'][$x], 'description' => $_SESSION['stageDescription'][$x], 'number' => $_SESSION['stageNumber'][$x]);
What I want to do is sort the values in the array by 'number'.

How do I do this?

Thanks


Mod | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Sorting array values

Post by EverLearning »

By using user defined comparison functions. Take a look at:

http://php.net/usort
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Sorting array values

Post by aceconcepts »

Mod | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have already looked at that page and I have search the net for answers.

However, I feel that my problem may lie in the fact that I have arrays with in one "parent" array.

here is some output from my array when printed:

Code: Select all

Array ( 
 [0] => Array ( [title] => Accommodation [description] => [number] => 1 ) 
 [1] => Array ( [title] => Personal [description] => [number] => 2 ) 
 [2] => Array ( [title] => Travel [description] => [number] => 3 ) 
)
I am trying to sort this array by the [number] values ascending.

Any ideas how I may cater for this situation?


Mod | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Sorting array values

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply