Sorting a multi array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Kirby

    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


  • Virgil Green

    #2
    Re: Sorting a multi array

    "Paul Kirby" <admin@initcorp .co.uk> wrote in message
    news:caabtn$qgl $1$8300dec7@new s.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


    Comment

    • Brad Kent

      #3
      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(a rfsort($array,a rray('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>arfso rt(</B><B>)</B> [1.00 - 2005/05/26]');
      if ( !is_array($fiel ds) )
      $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$qg l$1$8300dec7@ne ws.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]

      Comment

      • Paul Kirby

        #4
        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@DESPAMobsy dian.com> wrote in message
        news:C2nyc.98$K H5.73@newssvr23 .news.prodigy.c om...[color=blue]
        > "Paul Kirby" <admin@initcorp .co.uk> wrote in message
        > news:caabtn$qgl $1$8300dec7@new s.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]


        Comment

        Working...