合并多维 PHP 数组

Merge multidimensional PHP arrays

从这个foreach:

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'): ['name' => 'COMBIART PLUS', 'data' => [92, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92]].

我希望我已经很好地解释了我的问题。

谢谢

抱歉,我花了一些时间才回复此消息。

这可能不是最优雅的方法,但假设您想要组合具有相同名称的数组,并通过使用值和键(假设为零)将数据数组组合成一个唯一的数组在这里没有用),你可以这样做:

$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.

感谢你的帮助 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;
    }