473,498 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2958
"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
2071
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
1789
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
1434
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
3020
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
2101
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
3211
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
8965
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
5252
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
4901
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...
0
7121
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
6993
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
7197
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...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.