By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,349 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,349 IT Pros & Developers. It's quick & easy.

Sorting a multi array

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.