2016-04-14 9 views
-1

Ich habe das folgende Array aus einer Datenbank entnommen:Finden Sie doppelte Werte in einem mehrdimensionalen Array und neue mehrdimensionalen Matrix erstellen, die keine Duplikate enthalten

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

Und ich brauche es verschachtelt werden, wie folgt: :

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

um dies näher auszuführen: ich habe keinen doppelten Namen in meinem Array haben muß, und die Daten, die für den gleichen Namen existieren als neue Schlüssel, um den Daten-Array, das die gleichen n entsprechen eingegeben werden ame, und das gleiche für Zeitfenster. Wie kann ich das machen?

Antwort

1

Für diese Lösung lassen Sie uns das Array aus dem databse genommen sagen wird $facilities genannt. Der Code würde sein:

$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'] 
     ); 
    } 
} 
+0

Vielen Dank dafür !! Sowohl für die Zeit, um den Algorithmus und die Kommentare zu schreiben. Zumindest verstehe ich jetzt, wie das hätte gemacht werden sollen. – arch1ve

0

JSON Array wird Ihre Aufgabe sehr einfach machen, und wird in PHP unterstützt

Verwandte Themen