2015-03-31 192 views
1

從這個foreach合併多維數組PHP

foreach ($statisticheProdotti as $values) { 
     $prod[] = $values['nome']; 
     $nomi = array_values(array_unique($prod, SORT_REGULAR)); 
     $arr = array_combine(range(1, 12), range(1, 12)); 
     foreach ($arr as $key => $val) { 
      $data = ($val == $values['mese']) ? $values['data'] : 0; 
      $arr[$val] = $data; 
     } 
     $prodotti[] = ['name' => $values['nome'], 'data' => array_values($arr)]; 
    } 

我得到這個數組:

array (size=14) 
    0 => 
    array (size=2) 
     'name' => string 'COMBIART PLUS' (length=13) 
     'data' => 
     array (size=12) 
      0 => string '92' (length=2) 
      1 => int 0 
      2 => int 0 
      3 => int 0 
      4 => int 0 
      5 => int 0 
      6 => int 0 
      7 => int 0 
      8 => int 0 
      9 => int 0 
      10 => int 0 
      11 => int 0 
    1 => 
    array (size=2) 
     'name' => string 'COMBIART PLUS' (length=13) 
     'data' => 
     array (size=12) 
      0 => int 0 
      1 => int 0 
      2 => int 0 
      3 => int 0 
      4 => int 0 
      5 => int 0 
      6 => int 0 
      7 => int 0 
      8 => int 0 
      9 => int 0 
      10 => int 0 
      11 => string '92' (length=2) 
    2 => 
    array (size=2) 
     'name' => string 'SECUR' (length=10) 
     'data' => 
     array (size=12) 
      0 => string '5' (length=1) 
      1 => int 0 
      2 => int 0 
      3 => int 0 
      4 => int 0 
      5 => int 0 
      6 => int 0 
      7 => int 0 
      8 => int 0 
      9 => int 0 
      10 => int 0 
      11 => int 0 
    3 => 
    .....` 

我需要刪除重複name,並具有獨特的陣列數據。 最終輸出應該是(例如來自索引0和1的名稱相同): ['name' => 'COMBIART PLUS', 'data' => [92, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92]]

我希望我已經很好地解釋了我的問題。

謝謝

+1

你嘗試過什麼然而? – 2015-03-31 22:53:01

+0

是的,從一些例子開始,但沒有什麼好的。 – Cloud78 2015-04-01 07:11:28

回答

1

對不起,我花了一段時間迴應。

這可能不是最優雅的方法,但是假設您想要將相同名稱的數組合並在一起,並使用值和鍵組合數組來創建唯一數組(假設爲零這裏沒有用),你可以這樣做:

$output = array(); 
foreach ($example as $data) { 
    // Use the name as a quick array key reference to avoid having to search a second level of the array 
    $key = $data['name']; 
    if (!array_key_exists($key, $output)) { 
     // Instantiate the first entry using the name as the array key (we can reset this later) 
     $output[$key] = array(
      'name' => $key, 
      'data' => $data['data'] 
     ); 
     // Skip the rest of this loop 
     continue; 
    } 
    // An entry already exists for the name, so merge any unique values into the array using the 
    // keys AND values to determine if something is unique. Zero is treated as empty. 
    foreach ($data['data'] as $newKey => $newValue) { 
     if (empty($output[$key]['data'][$newKey])) { 
      // Add a new unique entry to the output array (zero counts as empty here) 
      $output[$key]['data'][$newKey] = $newValue; 
     } 
    } 
} 

// Remove the name from the keys now 
$output = array_values($output); 

這應該會給你你想要的結果。 Example here.

0

感謝您的幫助scrowler,感謝你的代碼,我已經能夠得到我需要的東西,但我只是改變的東西,以使其適應我的項目...

function generateChartData($array) { 
     $prodArr = []; 
     $oldProd = ''; 
     $arr = array_fill(0, 12, 0); 
     foreach ($array as $values) { 
      $Prod = $values['name']; 
      if ($Prod !== $oldProd) { 
       if (!empty($oldProd)) { 
        $prodotti[] = ['name' => $oldProd, 'data' => $arr]; 
       } 
       $oldProd = $Prod; 
      } 
      foreach ($arr as $key => $val) { 
       if ($key == $values['mese'] - 1) { 
        $arr[$key] = (int) $values['data']; 
       } 
      } 
     } 
     // get last item 
     if (!empty($oldProd)) { 
      $prodArr[] = ['name' => $oldProd, 'data' => $arr]; 
     } 
     return $prodArr; 
    }