简体   繁体   中英

Merge multidimensional PHP arrays

From this 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)];
    }

I get this array:

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 => 
    .....`

I need to remove duplicated name and have unique array data. Final output should be (example from index 0 and 1 which have same 'name'): ['name' => 'COMBIART PLUS', 'data' => [92, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 92]] .

I hope I've well explained my problem.

Thanks

Sorry it's taken me a while to respond to this.

This might not be the most elegant way to do this, but assuming you want to combine the arrays with the same name, and combine the data array to make a unique array by using the values and the keys (assuming zero is not useful here), you can do something like this:

$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);

This should give you the result you're after. Example here.

thanks for your help scrowler, thanks to your code I have been able to get what I needed but I've just changed something to adapt it to my project...

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM