473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
Jul 17 '05 #1
2 12025
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 :
Jul 17 '05 #2
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>';

?>
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
4
by: Brett | last post by:
I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array. ie. @l1={3,1,2};...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
1
by: Marcus Kwok | last post by:
I wrote a little test program to demonstrate what may be a misunderstanding on my part in the behavior of ArrayList::Sort(). I defined a class (Test) that has two data members: a System::String*...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.