sort array by key

This is a discussion on sort array by key within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, What I want is simple, but I can't figure it out at the moment. Let's say this ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-02-2004
Boefje
 
Posts: n/a
Default sort array by key

Hi,

What I want is simple, but I can't figure it out at the moment.

Let's say this is an array names $matches:

Array
(
[0] => Array
(
[teamid] => 137
[teamnaam] => Weet ik Veel ??
[speeldagid] => vr
[speeldagvolgnr] => 5
)

[1] => Array
(
[teamid] => 27
[teamnaam] => Tycoon
[speeldagid] => di
[speeldagvolgnr] => 2
)

[2] => Array
(
[teamid] => 36
[teamnaam] => Arabier 1
[speeldagid] => do
[speeldagvolgnr] => 4
)

[3] => Array
(
[teamid] => 105
[teamnaam] => Road Runners
[speeldagid] => do
[speeldagvolgnr] => 4
)
)

And I want to sort it on key 'speeldagvolgnr', how do I do this?
So I want the array back


Array
(
[0] => Array
(
[teamid] => 27
[teamnaam] => Tycoon
[speeldagid] => di
[speeldagvolgnr] => 2
)

[1] => Array
(
[teamid] => 36
[teamnaam] => Arabier 1
[speeldagid] => do
[speeldagvolgnr] => 4
)

[2] => Array
(
[teamid] => 105
[teamnaam] => Road Runners
[speeldagid] => do
[speeldagvolgnr] => 4
)
[3] => Array
(
[teamid] => 137
[teamnaam] => Weet ik Veel ??
[speeldagid] => vr
[speeldagvolgnr] => 5
)
)




I have looked at the sort functions at PHP.net, but could not find the
solution.

Can you help me out? Thanx.

Reply With Quote
  #2 (permalink)  
Old 06-02-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: sort array by key

Boefje wrote:
> I have looked at the sort functions at PHP.net, but could not find the
> solution.
>


You have missed usort():

function cmp($a, $b) {
if ($a['speeldagvolgnr'] == $b['speeldagvolgnr']) {
return 0;
}
return ($a['speeldagvolgnr'] < $b['speeldagvolgnr']) ? -1 : 1;
}

usort($matches, "cmp");


JW



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:38 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0