2016-07-15 5 views
1

Ich versuche, die doppelte Array-Wertgruppe basierend auf den gleichen Daten zu entfernen (nur 1 Gruppe behalten).Multidemensional Array filter out Duplikate innerhalb der Gruppe

Nehmen Sie auch die Nummer in der "1" -Taste (über der PID-Taste) und fügen Sie sie zusammen mit der duplizierten Gruppe hinzu.

Das Problem im Haben ist die jede Gruppe unter Stunde ist "nur" vermutet, mit doppelten Gruppen unter der gleichen "Stunde" Abschnitt zu filtern.

Hier ist das Array im beginnend mit:

Array(
    [1488] => Array 
     (
      [hours] => Array 
       (
        [0] => Array 
         (
          [0] => 2016/07/11 - 2016/07/17 
          [1] => 37 
          [pid] => 1488 
         ) 

        [1] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 51 
          [pid] => 1488 
         ) 

        [2] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 44 
          [pid] => 1488 
         ) 

        [3] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 22 
          [pid] => 1488 
         ) 

       ) 

     ) 

    [2] => Array 
     (
      [hours] => Array 
       (
        [0] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 9.5 
          [pid] => 2 
         ) 

        [1] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 8 
          [pid] => 2 
         ) 

        [2] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 7 
          [pid] => 2 
         ) 

        [3] => Array 
         (
          [0] => 2016/07/11 - 2016/07/17 
          [1] => 5 
          [pid] => 2 
         ) 

       ) 

     ) 

) 

Hier ist, was ich brauche es aussehen:

Array(
    [0] => Array 
     (
      [hours] => Array 
       (
        [0] => Array 
         (
          [0] => 2016/07/11 - 2016/07/17 
          [1] => 37 
          [pid] => 1488 
         ) 

        [1] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 117 
          [pid] => 1488 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [hours] => Array 
       (
        [0] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 24.5 
          [pid] => 2 
         ) 

        [1] => Array 
         (
          [0] => 2016/07/11 - 2016/07/17 
          [1] => 5 
          [pid] => 2 
         ) 

       ) 

     ) 

) 

Dies ist mein Code:

$temp_array = array(); 
    $w = 0; 
    $key_array = array(); 

    $new_array = array(); 

    foreach($project_hours as $old_arrs) { 


      foreach ($old_arrs["hours"] as $val) { 
       if (!in_array($val[0], $key_array)) { 
        $key_array[$w] = $val[0]; 
        $temp_array[$w] = $val; 

       } else { 
        $ser = array_search($val[0], $key_array); 
        $temp_array[$ser][1] += $val[1]; 
       } 

       $w++; 

       $old_arrs["hours"] = $temp_array; 

      } 


     $new_array[] = $old_arrs; 
    } 

    echo '<pre>'; 
    print_r($new_array); 
    echo '</pre>'; 
+2

Wenn dieses Array bauen Sie auch die '[0]' Wert als Index verwenden könnte und wenn das 'isset', fügen Sie einfach die' [1 ] 'zusammen. –

Antwort

1

Sie können nur das tun, einige einfache Tricks, um die Array-Schlüssel zu organisieren, dann verwenden Sie array_values() in zwei Bereichen, um die assoziativen Schlüssel auf n zurückzusetzen umeric Tasten:

// Your array 
$data = array(
    1488 => array(
     'hours' => array(
      0 => array(
       0 => '2016/07/11 - 2016/07/17', 
       1 => 37, 
       'pid' => 1488, 
      ), 
      1 => array(
       0 => '2016/07/04 - 2016/07/10', 
       1 => 51, 
       'pid' => 1488 
      ), 
      2 => array(
       0 => '2016/07/04 - 2016/07/10', 
       1 => 44, 
       'pid' => 1488 
      ), 
      3 => array(
       0 => '2016/07/04 - 2016/07/10', 
       1 => 22, 
       'pid' => 1488 
      ) 
     ) 
    ), 
    2 => array(
     'hours' => array(
      0 => array(
       0 => '2016/07/04 - 2016/07/10', 
       1 => 9.5, 
       'pid' => 2 
      ), 
      1 => array(
       0 => '2016/07/04 - 2016/07/10', 
       1 => 8, 
       'pid' => 2 
      ), 
      2 => array(
       0 => '2016/07/04 - 2016/07/10', 
       1 => 7, 
       'pid' => 2 
      ), 
      3 => array(
       0 => '2016/07/11 - 2016/07/17', 
       1 => 5, 
       'pid' => 2 
      ) 
     ) 
    ) 
); 

// Loop through array... 
foreach($data as $pid => $group) { 
    foreach($group['hours'] as $row) { 
     // I am using the date as a unique key so it's easy to organize 
     $new[$pid]['hours'][$row[0]][0] = $row[0]; 
     // This will draw an warning if you don't first check that it's set 
     if(!isset($new[$pid]['hours'][$row[0]][1])) 
      $new[$pid]['hours'][$row[0]][1] = 0; 
     // Add values together as you go 
     $new[$pid]['hours'][$row[0]][1]  += $row[1]; 
     // On each loop this just assigns the pid (it just keeps overwriting itself 
     // but that is no big deal) 
     $new[$pid]['hours'][$row[0]]['pid'] = $pid; 
    } 
    // Here since you are not using date keys, just reset to numeric 
    $new[$pid]['hours'] = array_values($new[$pid]['hours']); 
} 

print_r(array_values($new)); 

Gibt Ihnen:

Array 
(
    [0] => Array 
     (
      [hours] => Array 
       (
        [0] => Array 
         (
          [0] => 2016/07/11 - 2016/07/17 
          [1] => 37 
          [pid] => 1488 
         ) 

        [1] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 117 
          [pid] => 1488 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [hours] => Array 
       (
        [0] => Array 
         (
          [0] => 2016/07/04 - 2016/07/10 
          [1] => 24.5 
          [pid] => 2 
         ) 

        [1] => Array 
         (
          [0] => 2016/07/11 - 2016/07/17 
          [1] => 5 
          [pid] => 2 
         ) 

       ) 

     ) 

) 
+0

Sie sind ein Lebensretter. –

+0

Eigentlich lese ich gerade @ JonathanKuhn Kommentar. Er hat im Wesentlichen vorgeschlagen, was ich gerade getan habe, also hatte er zuerst die Idee! – Rasclatt

Verwandte Themen