Sorting a Two Dimensional Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • StevePBurgess@gmail.com

    #1

    Sorting a Two Dimensional Array

    Hi. I have an array, thus:

    Array (
    [0] => Array ( [Freq] => 28 [Name] => Tomato )
    [1] => Array ( [Freq] => 4 [Name] => Banana )
    [2] => Array ( [Freq] => 54 [Name] => Orange )
    [3] => Array ( [Freq] => 24 [Name] => Eggplant )
    [4] => Array ( [Freq] => 5 [Name] => Courgette )
    [5] => Array ( [Freq] => 4 [Name] => Apple )
    )

    I would like to be able to sort it either by the Frequency or the Name
    of the fruit or vegetable.

    I just can't seem to get it to work. I'm sorry - I realise that this is
    probably really easy but my brain isn't working today and the examples
    on php.net seem incomprehensibl e to me.

    Many thanks, in anticipation

    Steve

  • Janwillem Borleffs

    #2
    Re: Sorting a Two Dimensional Array

    StevePBurgess@g mail.com wrote:[color=blue]
    > I just can't seem to get it to work. I'm sorry - I realise that this
    > is probably really easy but my brain isn't working today and the
    > examples on php.net seem incomprehensibl e to me.
    >[/color]

    // $key = 'Freq';
    $key = 'Name';

    function callback($a, $b) {
    global $key;
    if ($a[$key] == $b[$key]) return 0;
    return $a[$key] > $b[$key] ? 1 : -1;
    }
    uasort($array, 'callback');


    JW


    Comment

    • StevePBurgess@gmail.com

      #3
      Re: Sorting a Two Dimensional Array


      Janwillem Borleffs wrote:[color=blue]
      >
      >
      > // $key = 'Freq';
      > $key = 'Name';
      >
      > function callback($a, $b) {
      > global $key;
      > if ($a[$key] == $b[$key]) return 0;
      > return $a[$key] > $b[$key] ? 1 : -1;
      > }
      > uasort($array, 'callback');
      >
      >[/color]

      Brilliant - many thanks.

      Steve

      Comment

      • Krustov

        #4
        Re: Sorting a Two Dimensional Array (2)

        <comp.lang.ph p>
        <>
        <26 Jun 2006 14:50:17 -0700>
        <1151358614.280 786.317070@b68g 2000cwa.googleg roups.com>
        [color=blue]
        > Brilliant - many thanks.
        >[/color]

        OP question answered .


        Can anything like the following be used for 2 dimentional arrays ? .

        $demo=[1,1]="blah";
        $demo=[1,2]="blah";


        --

        Comment

        • Krustov

          #5
          Re: Sorting a Two Dimensional Array (2)

          <comp.lang.ph p>
          <Krustov>
          <Tue, 27 Jun 2006 12:40:46 +0100>
          <MPG.1f0b298ba4 e24483989abe@ne ws.newsreader.c om>
          [color=blue]
          > Can anything like the following be used for 2 dimentional arrays ? .
          >
          > $demo=[1,1]="blah";
          > $demo=[1,2]="blah";
          >[/color]

          No answers provided for this question - so assume there isnt .


          --

          Comment

          • Jerry Stuckle

            #6
            Re: Sorting a Two Dimensional Array (2)

            Krustov wrote:[color=blue]
            > <comp.lang.ph p>
            > <Krustov>
            > <Tue, 27 Jun 2006 12:40:46 +0100>
            > <MPG.1f0b298ba4 e24483989abe@ne ws.newsreader.c om>
            >[color=green]
            >>Can anything like the following be used for 2 dimentional arrays ? .
            >>
            >>$demo=[1,1]="blah";
            >>$demo=[1,2]="blah";
            >>[/color]
            >
            >
            > No answers provided for this question - so assume there isnt .
            >
            >[/color]

            First of all, you need to give people a chance to answer - at least 24 hours.
            We're all volunteers and some only check in once a day.

            To answer your question, no.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • ImOk

              #7
              Re: Sorting a Two Dimensional Array (2)


              $xx=array(array (1,2),array(3,4 ));
              echo $xx[0][0]; // prints 1
              echo $xx[1][1]; // prints 4


              Krustov wrote:[color=blue]
              > <comp.lang.ph p>
              > <Krustov>
              > <Tue, 27 Jun 2006 12:40:46 +0100>
              > <MPG.1f0b298ba4 e24483989abe@ne ws.newsreader.c om>
              >[color=green]
              > > Can anything like the following be used for 2 dimentional arrays ? .
              > >
              > > $demo=[1,1]="blah";
              > > $demo=[1,2]="blah";
              > >[/color]
              >
              > No answers provided for this question - so assume there isnt .
              >
              >
              > --
              > www.emailuser.co.uk/?name=KRUSTOV[/color]

              Comment

              • ImOk

                #8
                Re: Sorting a Two Dimensional Array (2)


                ImOk wrote:[color=blue]
                > $xx=array(array (1,2),array(3,4 ));
                > echo $xx[0][0]; // prints 1
                > echo $xx[1][1]; // prints 4
                >[/color]

                Which also follows you can do this:

                $xx[0][0]="X";
                echo $xx[0][0];

                Comment

                • Krustov

                  #9
                  Re: Sorting a Two Dimensional Array (2)

                  <comp.lang.ph p>
                  <ImOk>
                  <27 Jun 2006 18:31:17 -0700>
                  <1151458277.277 011.159570@x69g 2000cwx.googleg roups.com>
                  [color=blue]
                  > $xx[0][0]="X";
                  > echo $xx[0][0];
                  >[/color]

                  Yeah that looks fine - thanks .


                  --

                  Comment

                  Working...