sort multi-dimensional array

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

    sort multi-dimensional array

    Hi,

    does anyone know how i can sort a multi-dimensional array
    by a specific field ?

    for example i want to sort arr[m][n] where n=2,
    but i need the data of every array to follow that order.

    example array:
    arr[0][0]
    arr[0][1]
    arr[0][2]
    arr[0][3]
    arr[0][4]
    arr[0][5]

    arr[1][0]
    arr[1][1]
    arr[1][2]
    arr[1][3]
    arr[1][4]
    arr[1][5]
  • gbbulldog@googlemail.com

    #2
    Re: sort multi-dimensional array

    Balaskas Evaggelos wrote:
    Hi,
    >
    does anyone know how i can sort a multi-dimensional array
    by a specific field ?
    >
    for example i want to sort arr[m][n] where n=2,
    but i need the data of every array to follow that order.
    >
    example array:
    arr[0][0]
    arr[0][1]
    arr[0][2]
    arr[0][3]
    arr[0][4]
    arr[0][5]
    >
    arr[1][0]
    arr[1][1]
    arr[1][2]
    arr[1][3]
    arr[1][4]
    arr[1][5]
    You can use the array_multisort () function to do this. More info and a
    number of examples of useage can be found at :


    Comment

    • Balaskas Evaggelos

      #3
      Re: sort multi-dimensional array

      gbbulldog@googl email.com wrote:
      Balaskas Evaggelos wrote:
      >Hi,
      >>
      >does anyone know how i can sort a multi-dimensional array
      >by a specific field ?
      >>
      >for example i want to sort arr[m][n] where n=2,
      >but i need the data of every array to follow that order.
      >>
      >example array:
      >arr[0][0]
      >arr[0][1]
      >arr[0][2]
      >arr[0][3]
      >arr[0][4]
      >arr[0][5]
      >>
      >arr[1][0]
      >arr[1][1]
      >arr[1][2]
      >arr[1][3]
      >arr[1][4]
      >arr[1][5]
      >
      You can use the array_multisort () function to do this. More info and a
      number of examples of useage can be found at :

      >
      thanks for your anwser but the first thing i checked,
      before i wrote this post, was that url.

      The example are for multi-dimensional array of one-dimensional arrays.

      I can't find how i can sort a multi-dimensional array of array[m][n]
      and for a specific field example n=2.

      Comment

      • mootmail-googlegroups@yahoo.com

        #4
        Re: sort multi-dimensional array

        Balaskas Evaggelos wrote:
        thanks for your anwser but the first thing i checked,
        before i wrote this post, was that url.
        >
        The example are for multi-dimensional array of one-dimensional arrays.
        >
        I can't find how i can sort a multi-dimensional array of array[m][n]
        and for a specific field example n=2.
        Maybe I just am not understanding you correctly, but aren't they the
        same thing?
        In PHP:
        $ar['m']['n'] = 1;
        is equivalent to:
        $ar = array('m' =array('n' =1));

        Both can be referenced as $ar['m']['n'].

        Off the top of my head, the most relevant example from that php manual
        page would be Example 3. Sorting database results. It sounds like you
        want to essentially sort a 'table' of data by one of the columns. The
        concepts of that example should point you in the right direction.
        Unless, as I said, I am completely misunderstandin g your question.

        Comment

        • Balaskas Evaggelos

          #5
          Re: sort multi-dimensional array

          mootmail-googlegroups@ya hoo.com wrote:
          Balaskas Evaggelos wrote:
          >thanks for your anwser but the first thing i checked,
          >before i wrote this post, was that url.
          >>
          >The example are for multi-dimensional array of one-dimensional arrays.
          >>
          >I can't find how i can sort a multi-dimensional array of array[m][n]
          >and for a specific field example n=2.
          >
          Maybe I just am not understanding you correctly, but aren't they the
          same thing?
          In PHP:
          $ar['m']['n'] = 1;
          is equivalent to:
          $ar = array('m' =array('n' =1));
          >
          Both can be referenced as $ar['m']['n'].
          >
          Off the top of my head, the most relevant example from that php manual
          page would be Example 3. Sorting database results. It sounds like you
          want to essentially sort a 'table' of data by one of the columns. The
          concepts of that example should point you in the right direction.
          Unless, as I said, I am completely misunderstandin g your question.
          >
          maybe i don't write it correctly - bad english :)

          in the example 3 the multi-dimensional array breaks into two
          one-dimensional arrays to sort them. So the results is
          sorted for every separated array.

          i need the 2 arrays to be sorted only by the second field of the second
          array. And all the rest to be sorted according by this field.

          this is my array:

          arr[0][0]
          arr[0][1]
          arr[0][2]
          arr[0][3]
          arr[0][4]
          arr[0][5]

          arr[1][0]
          arr[1][1]
          arr[1][2]
          arr[1][3]
          arr[1][4]
          arr[1][5]

          ....
          ....

          i want to sort the arr be second element into the arr:

          arr[m][2], and everything else will follow this order.

          ---
          as m: 0,1,2,3,4,5,6,7 ,8,9,...
          as n: 0,1,2,3,4,5,6,7 ,8,9,...

          Comment

          Working...