2015-03-31 150 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; 
    }