Array Sorting, 2 items...

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Array Sorting, 2 items...

    Hi,

    Trying to accomplish:

    I want to sort my array by two columns. The array is setup:

    Array
    (
    [0] => Array
    (
    [0] => CHECKING
    [ba_type] => CHECKING
    [1] => 10132200
    [loan_number] => 10132200
    [2] => 10000049
    [loan_id] => 10000049
    [3] => MONICA
    [first_name] => MONICA
    [4] => MERCHANT
    [last_name] => MERCHANT
    [5] => 2003-07-17 16:15:32
    [approved_date] => 2003-07-17 16:15:32
    [6] => ACH Debit
    [type] => ACH Debit
    )

    [1] => Array
    (
    [0] => CHECKING
    [ba_type] => CHECKING
    [1] => 10154654
    [loan_number] => 10154654
    [2] => 10000055
    [loan_id] => 10000055
    [3] => RICH
    [first_name] => RICH
    [4] => LABO
    [last_name] => LABO
    [5] => 2003-07-17 16:15:32
    [approved_date] => 2003-07-17 16:15:32
    [6] => ACH Debit
    [type] => ACH Debit
    )
    )

    What I'd like to happen is first sort by ba_type, and then sort by
    loan_number, so that I end up with a nice output similar to;

    CHECKING 101234
    CHECKING 101544
    CHECKING 101573
    SAVINGS 101112
    SAVINGS 101224
    VISA 101110
    VISA 101998

    Keep everything groupped together in their respective ba_type, and put the
    numbers in numeric order. I've looked at usort, and many examples of it. I
    can see how to use it to sort one column, but how would I sort a second to
    achieve this?

    -Dan Joseph

    Dan Joseph Guest

  2. Similar Questions and Discussions

    1. Sorting array vs sorting paginated array
      ....pulling in a long list of photos in a gallery, and I have a sort function working within the pages of data fine. I need to bring it back out of...
    2. Sorting tree items manually
      I am trying to allow the user a way of shifting through items in a tree 2 levels deep. This will give the user a chance to order the items within...
    3. Array Sorting
      Hey everyone, I figured someone out there must have come across the need for this before, so rather than continue banging my head against the...
    4. client side sorting of datagrid items
      Does anyone know if there's code out there to manage client-side sorting of datagrid items? I'd like to be able to fill a datagrid with records...
    5. [PHP] Array Sorting, 2 items...
      > -----Original Message----- Well, erm, maybe I've got the wrong glasses on today, or maybe you've made a cut'n'paste boo-boo, but it sure looks...
  3. #2

    Default Re: [PHP] Array Sorting, 2 items...

    Your array seems like a result of mysql_fetch_array(), cannot you order
    it in the sql query?

    Dan Joseph wrote:
    > Array
    > (
    > [0] => Array
    > (
    > [0] => CHECKING
    > [ba_type] => CHECKING
    > [1] => 10132200
    > [loan_number] => 10132200
    > Keep everything groupped together in their respective ba_type, and put the
    > numbers in numeric order. I've looked at usort, and many examples of it. I
    > can see how to use it to sort one column, but how would I sort a second to
    > achieve this?
    Simply if ba_types are equal, continue with checking loan_numbers:

    function cmp ($a, $b) {
    if ($a['ba_type'] == $b['ba_type']) {
    if ($a['loan_number'] == $b['loan_number']) return 0;
    return ($a['loan_number'] > $b['loan_number']) ? -1 : 1;
    }
    return ($a['ba_type'] > $b['ba_type']) ? -1 : 1;
    }

    Marek Kilimajer Guest

  4. #3

    Default RE: [PHP] Array Sorting, 2 items...

    Hi,

    You know, this worked just fine, as did my order by. Its my brain that is
    completely wacked. I am going about this all wrong....

    Thanks for all your help.. Take care.. I'll let you know how it goes.

    -Dan Joseph
    > -----Original Message-----
    > From: Marek Kilimajer [mailto:kilimajer@webglobe.sk]
    > Sent: Tuesday, July 22, 2003 11:25 AM
    > To: Dan Joseph
    > Cc: [email]php-general@lists.php.net[/email]
    > Subject: Re: [PHP] Array Sorting, 2 items...
    >
    >
    > Your array seems like a result of mysql_fetch_array(), cannot you order
    > it in the sql query?
    >
    > Dan Joseph wrote:
    > > Array
    > > (
    > > [0] => Array
    > > (
    > > [0] => CHECKING
    > > [ba_type] => CHECKING
    > > [1] => 10132200
    > > [loan_number] => 10132200
    >
    > > Keep everything groupped together in their respective
    > ba_type, and put the
    > > numbers in numeric order. I've looked at usort, and many
    > examples of it. I
    > > can see how to use it to sort one column, but how would I sort
    > a second to
    > > achieve this?
    > Simply if ba_types are equal, continue with checking loan_numbers:
    >
    > function cmp ($a, $b) {
    > if ($a['ba_type'] == $b['ba_type']) {
    > if ($a['loan_number'] == $b['loan_number']) return 0;
    > return ($a['loan_number'] > $b['loan_number']) ? -1 : 1;
    > }
    > return ($a['ba_type'] > $b['ba_type']) ? -1 : 1;
    > }
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    Dan Joseph Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139