sort array by key

This is a discussion on sort array by key within the PHP Language 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 > PHP Language

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
Pedro Graca
 
Posts: n/a
Default Re: sort array by key

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


Try usort()
http://www.php.net/usort

> Let's say this is an array names $matches:
>
> Array
> (
> [0] => Array
> (
> [teamid] => 137
> [teamnaam] => Weet ik Veel ??
> [speeldagid] => vr
> [speeldagvolgnr] => 5
> )

(snip)

> And I want to sort it on key 'speeldagvolgnr', how do I do this?

(snip)

> I have looked at the sort functions at PHP.net, but could not find the solution.
>
> Can you help me out? Thanx.


Not tested

<?php
function sort_matches($left, $rite) {
if ($left['speeldagvolgnr'] == $rite['speeldagvolgnr']) return 0;
return $left['speeldagvolgnr'] < $rite['speeldagvolgnr'] ? -1 : 1;
}

// populate $matches
usort($matches, 'sort_matches');
// print_r($matches);
?>

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Reply With Quote
  #3 (permalink)  
Old 06-03-2004
Boefje
 
Posts: n/a
Default Re: sort array by key

Indeed! Thanks for pointing me in the right direction.

It can be done more simple. See next example.

<?php

function multi_sort($array, $key)
{
function compare($a, $b)
{
return strcmp($a[$key], $b[$key]);
}
usort($array, "compare");
return $array;
}

$fruits[0]["fruit"] = "lemons";
$fruits[0]["vendor"] = "Jansens lemons";
$fruits[1]["fruit"] = "apples";
$fruits[1]["vendor"] = "De Bruins apples";
$fruits[2]["fruit"] = "grapes";
$fruits[2]["vendor"] = "Pietersens grapes";

multi_sort($fruits, "fruit");

echo '<pre>';
print_r($fruits);
echo '</pre>';

?>
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 08:38 AM.


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