0

How can I sort this first by Member id and then by Payment date?

Array
(
    [240] => Array
        (
            [Member] => Array
                (
                    [id] => 112
                )

            [Payment] => Array
                (
                    [date] => 0712


)
    )

I tried with multisort, but I never found a way what was working and all examples I found didn't had my additional level.

PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
user1555112
  • 1,897
  • 6
  • 24
  • 43
  • look at this post : http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date – ppm blackhat Jul 26 '12 at 15:02

2 Answers2

2

Is the date value a string or an integer?

Anyway, supposing that date is an int, you could try this:

function my_sort($val1, $val2) {
    $compare_id = $val1['Member']['id'] - $val2['Member']['id'];
    if($compare_id == 0) {
        return $val1['Payment']['date'] - $val2['Payment']['date'];
    }
    else return $compare_id;
}

and then call:

usort($array, 'my_sort');
Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
0

If the data is received from database you can use ... ORDER BY member,date

  • And, from the looks of it, you're using CakePHP? You just have to include those in your array (at the same level as 'conditions') – tigertrussell Jul 26 '12 at 15:01