Connecting Tech Pros Worldwide Forums | Help | Site Map

Sorting a multi array

Paul Kirby
Guest
 
Posts: n/a
#1: Jul 17 '05
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



Virgil Green
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Sorting a multi array


"Paul Kirby" <admin@initcorp.co.uk> wrote in message
news:caabtn$qgl$1$8300dec7@news.demon.co.uk...[color=blue]
> Hello All
>
> I am trying to update me code to use arrays to store a group of[/color]
information[color=blue]
> 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 :([/color]

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

- Virgil


Brad Kent
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Sorting a multi array


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" <admin@initcorp.co.uk> wrote in message news:<caabtn$qgl$1$8300dec7@news.demon.co.uk>...[color=blue]
> 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[/color]
Paul Kirby
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Sorting a multi array


Thanks to both of you that helped a little, had to alter bits of their
examples to fit my needs :)

Paul Kirby

"Virgil Green" <vjg@DESPAMobsydian.com> wrote in message
news:C2nyc.98$KH5.73@newssvr23.news.prodigy.com...[color=blue]
> "Paul Kirby" <admin@initcorp.co.uk> wrote in message
> news:caabtn$qgl$1$8300dec7@news.demon.co.uk...[color=green]
> > Hello All
> >
> > I am trying to update me code to use arrays to store a group of[/color]
> information[color=green]
> > 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 :([/color]
>
> See www.php.net/uasort and associated functions.
>
> - Virgil
>
>[/color]


Closed Thread