sort array by key

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

    sort array by key

    Hi,

    What I want is simple, but I can't figure it out at the moment.

    Let's say this is an array names $matches:

    Array
    (
    [0] => Array
    (
    [teamid] => 137
    [teamnaam] => Weet ik Veel ??
    [speeldagid] => vr
    [speeldagvolgnr] => 5
    )

    [1] => Array
    (
    [teamid] => 27
    [teamnaam] => Tycoon
    [speeldagid] => di
    [speeldagvolgnr] => 2
    )

    [2] => Array
    (
    [teamid] => 36
    [teamnaam] => Arabier 1
    [speeldagid] => do
    [speeldagvolgnr] => 4
    )

    [3] => Array
    (
    [teamid] => 105
    [teamnaam] => Road Runners
    [speeldagid] => do
    [speeldagvolgnr] => 4
    )
    )

    And I want to sort it on key 'speeldagvolgnr ', how do I do this?
    So I want the array back


    Array
    (
    [0] => Array
    (
    [teamid] => 27
    [teamnaam] => Tycoon
    [speeldagid] => di
    [speeldagvolgnr] => 2
    )

    [1] => Array
    (
    [teamid] => 36
    [teamnaam] => Arabier 1
    [speeldagid] => do
    [speeldagvolgnr] => 4
    )

    [2] => Array
    (
    [teamid] => 105
    [teamnaam] => Road Runners
    [speeldagid] => do
    [speeldagvolgnr] => 4
    )
    [3] => Array
    (
    [teamid] => 137
    [teamnaam] => Weet ik Veel ??
    [speeldagid] => vr
    [speeldagvolgnr] => 5
    )
    )




    I have looked at the sort functions at PHP.net, but could not find the solution.

    Can you help me out? Thanx.
  • Pedro Graca

    #2
    Re: sort array by key

    Boefje wrote:[color=blue]
    > What I want is simple, but I can't figure it out at the moment.[/color]

    Try usort()
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

    [color=blue]
    > Let's say this is an array names $matches:
    >
    > Array
    > (
    > [0] => Array
    > (
    > [teamid] => 137
    > [teamnaam] => Weet ik Veel ??
    > [speeldagid] => vr
    > [speeldagvolgnr] => 5
    > )[/color]
    (snip)
    [color=blue]
    > And I want to sort it on key 'speeldagvolgnr ', how do I do this?[/color]
    (snip)
    [color=blue]
    > I have looked at the sort functions at PHP.net, but could not find the solution.
    >
    > Can you help me out? Thanx.[/color]

    Not tested

    <?php
    function sort_matches($l eft, $rite) {
    if ($left['speeldagvolgnr '] == $rite['speeldagvolgnr ']) return 0;
    return $left['speeldagvolgnr '] < $rite['speeldagvolgnr '] ? -1 : 1;
    }

    // populate $matches
    usort($matches, 'sort_matches') ;
    // print_r($matche s);
    ?>

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Boefje

      #3
      Re: sort array by key

      Indeed! Thanks for pointing me in the right direction.

      It can be done more simple. See next example.

      <?php

      function multi_sort($arr ay, $key)
      {
      function compare($a, $b)
      {
      return strcmp($a[$key], $b[$key]);
      }
      usort($array, "compare");
      return $array;
      }

      $fruits[0]["fruit"] = "lemons";
      $fruits[0]["vendor"] = "Jansens lemons";
      $fruits[1]["fruit"] = "apples";
      $fruits[1]["vendor"] = "De Bruins apples";
      $fruits[2]["fruit"] = "grapes";
      $fruits[2]["vendor"] = "Pietersens grapes";

      multi_sort($fru its, "fruit");

      echo '<pre>';
      print_r($fruits );
      echo '</pre>';

      ?>

      Comment

      Working...