简体   繁体   中英

Sorting similar result in usort in PHP

I understand that this code is sorting up activity array the by latest start time and also just display which any activity that come first in the loop that have same date.

foreach ($activity_array AS $k => $v):
......

usort($v['activity'], function ($a, $b) {
$ad = new DateTime($a['start']);
$bd = new DateTime($b['start']);
if ($ad == $bd) {
return 0;
}
return $ad < $bd ? -1 : 1;
});

How can i sort it if the start time is the same, but got another venue variable that needed to be sort within the same activity.

|  Activity  |  Start         |   Venue   |
+------------+----------------+-----------+
| Activity A | 22/10/17 08.30 | Floor 1   |
| Activity D | 22/10/17 10.30 | Hall 3    |
| Activity B | 22/10/17 10.30 | Hall 1    |
| Activity C | 22/10/17 10.30 | Hall 2    |
| Activity X | 22/10/17 09.30 | Floor 2   |

to be like:

|  Activity  |  Start         |   Venue   |
+------------+----------------+-----------+
| Activity A | 22/10/17 08.30 | Floor 1   |
| Activity X | 22/10/17 09.30 | Floor 2   |
| Activity B | 22/10/17 10.30 | Hall 1    |
| Activity C | 22/10/17 10.30 | Hall 2    |
| Activity D | 22/10/17 10.30 | Hall 3    |

You can check again for venue when start time is same

foreach ($activity_array AS $k => $v):
......

usort($v['activity'], function ($a, $b) {
$ad = new DateTime($a['start']);
$bd = new DateTime($b['start']);
if ($ad == $bd) {

    $venue1 = $a['venue'];
    $venue2 = $b['venue'];
    return strcmp($venue1, $venue2);
}
return $ad < $bd ? -1 : 1;
});

Here is your solution....

$array = array(
        array('activity' => 'Activity A', 'start' => '22/10/17 08.30', 'venue' => 'Floor 1'),
        array('activity' => 'Activity D', 'start' => '22/10/17 10.30', 'venue' => 'Hall 3'),
        array('activity' => 'Activity B', 'start' => '22/10/17 10.30', 'venue' => 'Hall 1'),
        array('activity' => 'Activity C', 'start' => '22/10/17 10.30', 'venue' => 'Hall 2'),
        array('activity' => 'Activity X', 'start' => '22/10/17 09.30', 'venue' => 'Floor 2')
);  //Your Array
usort($array,"cmp");
function cmp($a, $b){
    if ($a["venue"] == $b["venue"]) {
        return 0;
    }
    return ($a["venue"] < $b["venue"]) ? -1 : 1;
}

Result

Array
(
[0] => Array
    (
        [activity] => Activity A
        [start] => 22/10/17 08.30
        [venue] => Floor 1
    )

[1] => Array
    (
        [activity] => Activity X
        [start] => 22/10/17 09.30
        [venue] => Floor 2
    )

[2] => Array
    (
        [activity] => Activity B
        [start] => 22/10/17 10.30
        [venue] => Hall 1
    )

[3] => Array
    (
        [activity] => Activity C
        [start] => 22/10/17 10.30
        [venue] => Hall 2
    )

[4] => Array
    (
        [activity] => Activity D
        [start] => 22/10/17 10.30
        [venue] => Hall 3
    )

)

Duplicate

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