Resorting an array?

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

    Resorting an array?

    I'm not even sure this is possible, and if it is, I'm having a hard
    time focusing on a function that would make it work.

    What I have is a dynamically generated form with dozens of rows from a
    mySQL query.
    Each row in this form has a checkbox to select what rows the user
    wants sent to the processing page.
    Each row also has a slew of various text fields and dropdown menus
    with data from the query and editable by the user.
    The user checks the boxes to send on.

    Then, on the process page, an array is created based on which
    checkboxes are selected.

    Now at the moment the process page processes each selected row on a
    for-each in the order they were listed on the previous page.

    Keeping the mySQL sort order the same as it is on the 1st page, is
    there a way to resort the array on the process page so that the rows
    are processed in a different order, based on one of the fields in the
    array?

    For example, each row has the following data:

    checkbox value(which is also the row
    id),firstname,l astname,address ,size,color

    And the array could have dozens of rows. Right now it's processed in
    order of checkbox value because that's how it's sorted on page one.
    What if I want to change the sort in the array by lastname before
    processing?

    Did that make any sense?

    I looked up asort() and usort() and they LOOK like something that
    might work...at least I understand what they're doing, but I'm not
    sure if something like that would work in this complicated (maybe?) of
    an array I have going.
    And to be honest, I have no idea in my array what is the key (the
    value of the checkbox selected?) and what happens when you have more
    than 2 items in each row of the array?

    If one of the ?sort() types IS what I need to look into, someone let
    me know? If there's another concept that would work better, someone
    clue me in?
    I'd appreciate it!
    Thanks,
    Liam
  • Erwin Moller

    #2
    Re: Resorting an array?

    LRW wrote:
    [color=blue]
    > I'm not even sure this is possible, and if it is, I'm having a hard
    > time focusing on a function that would make it work.
    >
    > What I have is a dynamically generated form with dozens of rows from a
    > mySQL query.
    > Each row in this form has a checkbox to select what rows the user
    > wants sent to the processing page.
    > Each row also has a slew of various text fields and dropdown menus
    > with data from the query and editable by the user.
    > The user checks the boxes to send on.
    >
    > Then, on the process page, an array is created based on which
    > checkboxes are selected.
    >
    > Now at the moment the process page processes each selected row on a
    > for-each in the order they were listed on the previous page.
    >
    > Keeping the mySQL sort order the same as it is on the 1st page, is
    > there a way to resort the array on the process page so that the rows
    > are processed in a different order, based on one of the fields in the
    > array?
    >
    > For example, each row has the following data:
    >
    > checkbox value(which is also the row
    > id),firstname,l astname,address ,size,color
    >
    > And the array could have dozens of rows. Right now it's processed in
    > order of checkbox value because that's how it's sorted on page one.
    > What if I want to change the sort in the array by lastname before
    > processing?
    >
    > Did that make any sense?
    >
    > I looked up asort() and usort() and they LOOK like something that
    > might work...at least I understand what they're doing, but I'm not
    > sure if something like that would work in this complicated (maybe?) of
    > an array I have going.
    > And to be honest, I have no idea in my array what is the key (the
    > value of the checkbox selected?) and what happens when you have more
    > than 2 items in each row of the array?
    >
    > If one of the ?sort() types IS what I need to look into, someone let
    > me know? If there's another concept that would work better, someone
    > clue me in?
    > I'd appreciate it!
    > Thanks,
    > Liam[/color]

    Hi Liam,

    I think I understand what you want.
    Only because i stubled on a similar problem recently. :-)

    Your problem is that you want a bunch of associated data sorted on one of
    the values of one item in those bunches of data, right?
    This is how you can manage that: associative arrays (you guessed) but just a
    little different than you tried. :-)

    in your receiving script try this:
    1) store all your rows in arrays.
    eg:
    loop over your Postdata{
    $myRow[1]["var1"] = $_POST["var1"];
    $myRow[1]["var2"] = $_POST["var2"];
    $myRow[1]["var3"] = $_POST["var3"];
    }

    ok?
    the [1] respresents a row with data. (Use 2 for second row, etc.)
    the ["varX"] respresents the value for a certain variable for that row.

    2) Now create a new assoc array and use for the key the value where you want
    to sort on.
    So suppose you need var2 for sorting:
    for all your $myRow{
    $allData[$var3-from-RowX] = $myRow[X]
    }

    3) Now you have a fresh datastructure that has keys based on var3.
    You can sort it now with asort.

    Hope this helps you.

    Maybe there is a smarter/faster way, but I like this way because I
    understand how it is working. :-)

    Good luck,
    Erwin Moller

    Comment

    • Erwin Moller

      #3
      Re: Resorting an array?

      Erwin Moller wrote:

      topy correction:
      [color=blue]
      > So suppose you need var2 for sorting:[/color]

      should of course be:
      [color=blue]
      > So suppose you need var3 for sorting:[/color]

      Comment

      • Michael Fesser

        #4
        Re: Resorting an array?

        .oO(Erwin Moller)
        [color=blue]
        >Your problem is that you want a bunch of associated data sorted on one of
        >the values of one item in those bunches of data, right?[/color]

        You can use array_multisort () to create a kind of column sort function:

        function csort($array, $column) {
        $s = array();
        foreach($array as $row) {
        $s[] = $row[$column];
        }
        array_multisort ($s, SORT_ASC, $array);
        return $array;
        }


        Example:

        $foo = array(
        array('firstnam e' => 'foo', 'lastname' => 'bar'),
        array('firstnam e' => 'bar', 'lastname' => 'foo')
        );

        print_r(csort($ foo, 'firstname'));


        Output:

        Array
        (
        [0] => Array
        (
        [firstname] => bar
        [lastname] => foo
        )

        [1] => Array
        (
        [firstname] => foo
        [lastname] => bar
        )

        )

        HTH
        Micha

        Comment

        • Erwin Moller

          #5
          Re: Resorting an array?

          Michael Fesser wrote:
          [color=blue]
          > .oO(Erwin Moller)
          >[color=green]
          >>Your problem is that you want a bunch of associated data sorted on one of
          >>the values of one item in those bunches of data, right?[/color]
          >
          > You can use array_multisort () to create a kind of column sort function:
          >[/color]

          <snip>

          Hee Micha,

          That is cool. :-)
          Never ever saw that function.
          I'll check it.

          Thanks,

          Regards,
          Erwin Moller

          Comment

          Working...