简体   繁体   中英

PHP sort multi dimensional array

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.

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');

如果从数据库收到数据,您可以使用... ORDER BY member,date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM