472,990 Members | 3,225 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Sorting a multi array

Hello All

I am trying to update me code to use arrays to store a group of information
and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(

If you require more information, then let me know.

Thanks in advance
Paul Kirby
Jul 17 '05 #1
3 2934
"Paul Kirby" <ad***@initcorp.co.uk> wrote in message
news:ca*******************@news.demon.co.uk...
Hello All

I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(


See www.php.net/uasort and associated functions.

- Virgil
Jul 17 '05 #2
I think I found a variant of this under the usort function:

$sorted_array = arfsort($array,'count');
or
$sorted_array = arfsort($array,array('count')); // can pass secondary
sort field in the 2nd param..

to get descending:
$sorted_array = array_reverse(arfsort($array,array('count')));

//arfsort() - (AR)ray (F)ield Sort.
//Sort a multi-dimensional array according to a list of fields.
//preserves keys
function arfsort( $array, $fields ) {
debug('<B>arfsort(</B><B>)</B> [1.00 - 2005/05/26]');
if ( !is_array($fields) )
$fields = array($fields);
$GLOBALS['__ARFSORT_LIST__'] = $fields;
uasort( $array, 'arfsort_func' );
unset($GLOBALS['__ARFSORT_LIST__']);
return $array;
}
//Internal sorting function for arfsort()
function arfsort_func( $a1, $a2 ) {
foreach( $GLOBALS['__ARFSORT_LIST__'] as $f ) {
if ( isset($a1[$f]) && isset($a2[$f]) ) {
$strc = strnatcasecmp( $a1[$f], $a2[$f] );
return $strc;
}
}
return 0;
}

"Paul Kirby" <ad***@initcorp.co.uk> wrote in message news:<ca*******************@news.demon.co.uk>...
Hello All

I am trying to update me code to use arrays to store a group of information
and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(

If you require more information, then let me know.

Thanks in advance
Paul Kirby

Jul 17 '05 #3
Thanks to both of you that helped a little, had to alter bits of their
examples to fit my needs :)

Paul Kirby

"Virgil Green" <vj*@DESPAMobsydian.com> wrote in message
news:C2*************@newssvr23.news.prodigy.com...
"Paul Kirby" <ad***@initcorp.co.uk> wrote in message
news:ca*******************@news.demon.co.uk...
Hello All

I am trying to update me code to use arrays to store a group of

information
and I have come up with a problem sorting the multiple array :(

Array trying to sort: (7 arrays put into an array) and I want to sort on
[count] Descending.
This is displayed using print_r() function.

Array
(
[czero] => Array
(
[game_type] => czero
[game_label] => Condition Zero
[count] => 2
)
[cstrike] => Array
(
[game_type] => cstrike
[game_label] => Counter-Strike
[count] => 3
)
[dod] => Array
(
[game_type] => dod
[game_label] => Day of Defeat
[count] => 1
)
[valve] => Array
(
[game_type] => valve
[game_label] => Half Life
[count] => 0
)
[gearbox] => Array
(
[game_type] => gearbox
[game_label] => Opposing Force
[count] => 0
)
[ricochet] => Array
(
[game_type] => ricochet
[game_label] => Ricochet
[count] => 0
)
[tfc] => Array
(
[game_type] => tfc
[game_label] => Team Fortress Classic
[count] => 1
)
)

Does anyone know how to do this ?
I have tried various array sorting functions without success :(


See www.php.net/uasort and associated functions.

- Virgil

Jul 17 '05 #4

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

Similar topics

11
by: Alan Searle | last post by:
I have been trying to get an array to sort on a particular column and have been having problems with my main key ... array_multisort($dirlist, SORT_DESC, SORT_STRING); This function takes an...
5
by: Eclectic | last post by:
Hey all, I have been using usort to sort my multi dimensional arrays ... function cmp($a, $b){ if($a == $b){ return 0; } return ($a < $b) ? -1 : 1; }
1
by: Jochen Califice | last post by:
hello ng! got a problem sorting my multi-dimensional array. I have the following situation of an array: $fahrzeugarray= "23" $fahrzeugarray="Audi TT" $fahrzeugarray= "34" $fahrzeugarray="VW...
7
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to...
2
by: lgbjr | last post by:
Hi All, Is there a way to sort multiple dimensions of an array in VB.NET? Let's say I have the following in a multi-dimension array: 3081 100 2 3081 100 1 3081 20 1 3081 1 2 3021 100 2
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
1
by: Christopher Miles | last post by:
Hello, I am trying to sort this array by the time field , and have all the other fields move accordingly to match .. Group_Array(0) - name Group_Array(1) - severity Group_Array(2) - Time...
5
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.