Page 1 of 1

Sorting array values

Posted: Thu Mar 06, 2008 4:44 am
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.

Re: Sorting array values

Posted: Thu Mar 06, 2008 7:03 am
by EverLearning
By using user defined comparison functions. Take a look at:

http://php.net/usort

Re: Sorting array values

Posted: Thu Mar 06, 2008 7:54 am
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.

Re: Sorting array values

Posted: Thu Mar 06, 2008 9:51 am
by pickle