在多维数组中查找重复值,然后创建不包含重复数据的新多维数组


Find duplicate values in a multidimensional array and create new multidimensional array that contains no dupes

我从数据库中获取了以下数组:

Array
(
[0] => Array
    (
        [facility_name] => AFC Clayton Juniors
        [day] => 15
        [month] => Apr
        [year] => 2016
        [start_time] => 20
        [end_time] => 21
    )
[1] => Array
    (
        [facility_name] => AFC Clayton Juniors
        [day] => 15
        [month] => Apr
        [year] => 2016
        [start_time] => 22
        [end_time] => 23
    )
[2] => Array
    (
        [facility_name] => Chorlton Runners
        [day] => 15
        [month] => Apr
        [year] => 2016
        [start_time] => 10
        [end_time] => 11
    )
[3] => Array
    (
        [facility_name] => Chorlton Runners
        [day] => 15
        [month] => Apr
        [year] => 2016
        [start_time] => 19
        [end_time] => 20
    )
)

我需要它嵌套如下::

Array
(
[0] => Array
    (
        [facility_name] => AFC Clayton Juniors
        [dates] => Array
               (
                    [date1] => Array
                            (
                                [date] => 15 Apr 2016
                                [timeslots] => Array
                                            (
                                              [timeslot1] => Array
                                                          (
                                                            [start_time] => 20
                                                            [end_time] => 21
                                                           )
                                              [timeslot2] => Array
                                                          (
                                                            [start_time] => 22
                                                            [end_time] => 23
                                                           )
                                            )
                    [date2] => 16 Apr 2016    //etc
              )
     )
[1] => Array
    (
      [facility_name] = Chorlton Runners
      [dates] => Array //etc
    )
)

详细说明:我需要在我的数组中没有重复的名称,并且存在的相同名称的日期将作为与相同名称匹配的日期数组的新关键字输入,对于时隙也是一样的。我该怎么做?

对于这个解决方案,假设从数据库中获取的数组命名为$facilities。代码是:

$noDupes = array();
foreach ($facilities as $fac) {
    $facilityIndex = -1; // The index of the facility name, -1 indicates it wasn't found.
    $dateIndex = '';     // The index of the date string, an empty string indicates it wasn't found.
    $timeslotIndex = ''; // The index of the timeslot, an empty string indicates it wasn't found.
    $facDate = "{$fac['day']} {$fac['month']} {$fac['year']}"; // The date string (dd mmm aaaa)
    foreach ($noDupes as $f => $facility) {
         if ($fac['facility_name'] == $facility['facility_name']) {
            // If the facility name was found we take the corresponding index (0, 1, 2, etc.).
            $facilityIndex = $f;
            foreach ($facility['dates'] as $d => $date) {
                if ($facDate == $date['date']) {
                    // If the date string was found we take the corresponding index (date1, date2, date3, etc.).
                    $dateIndex = $d;
                    foreach ($date['timeslots'] as $t => $timeslot) {
                        if ($fac['start_time'] == $timeslot['start_time'] && $fac['end_time'] == $timeslot['end_time']) {
                            // If the timeslot was found we take the corresponding index (timeslot1, timeslot2, timeslot3, etc.).
                            $timeslotIndex = $t;
                            break; // end timeslot loop
                        }
                    }
                    break; // end date loop
                }
            }
            break; // end facility loop
        }
    }
    if ($facilityIndex == -1) {
        // Take the new index for the date and timeslot if-statements
        $facilityIndex = count($noDupes);
        $noDupes[] = array(
            'facility_name' => $fac['facility_name'],
            'dates' => array()
        );
    }
    if ($dateIndex == '') {
        // Calculate the new index for the date (date1, date2, etc.)
        $dateNum = count($noDupes[$facilityIndex]['dates']) + 1;
        $dateIndex = "date{$dateNum}";
        $noDupes[$facilityIndex]['dates'][$dateIndex] = array(
            'date' => $facDate,
            'timeslots' => array()
        );
    }
    if ($timeslotIndex == '') {
        // Calculate the new index for the timeslot (timeslot1, timeslot2, etc.)
        $timeslotNum = count($noDupes[$facilityIndex]['dates'][$dateIndex]['timeslots']) + 1;
        $timeslotIndex = "timeslot{$timeslotNum}";
        $noDupes[$facilityIndex]['dates'][$dateIndex]['timeslots'][$timeslotIndex] = array(
            'start_time' => $fac['start_time'],
            'end_time' => $fac['end_time']
        );
    }
}

使用JSON数组将使您的任务变得非常简单,并且在PHP 中得到支持