合并多维PHP数组

时间:2015-03-31 16:46:14

标签: php arrays multidimensional-array

从此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' => 'COMBIART PLUS', 'data' => [92, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92]]

我希望我能很好地解释我的问题。

由于

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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;
    }