&$row) { if (!isset($row['data']) || !is_array($row['data'])) { error_log("Routine field_id 347: invalid row structure at index {$rowIndex}."); continue; } $selectedValues = []; /* * Check columns from P to AT. * If the cell contains x, take the related column header. */ for ($columnIndex = $startColumnIndex; $columnIndex <= $endColumnIndex; $columnIndex++) { $cellValue = strtolower(trim((string)($row['data'][$columnIndex] ?? ''))); if ($cellValue === 'x') { $headerTitle = trim((string)($headers[$columnIndex] ?? '')); if ($headerTitle !== '') { $selectedValues[] = $headerTitle; } } } /* * Add free text from column AU. */ $extraText = ''; if (isset($row['data'][$extraColumnIndex])) { $extraText = trim((string)$row['data'][$extraColumnIndex]); } elseif (isset($row['data']['AU'])) { $extraText = trim((string)$row['data']['AU']); } error_log( "Routine field_id 347: row " . ($row['excelrow'] ?? $rowIndex) . " AU index {$extraColumnIndex} value: " . print_r($row['data'][$extraColumnIndex] ?? null, true) . " | AU key value: " . print_r($row['data']['AU'] ?? null, true) ); if ($extraText !== '') { $selectedValues[] = $extraText; } /* * Remove empty and duplicate values. */ $selectedValues = array_values(array_unique(array_filter($selectedValues, function ($value) { return trim((string)$value) !== ''; }))); /* * Save final value into column P. * Column P must be mapped to field_id 347 in the template mapping. */ $row['data'][$targetColumnIndex] = implode(', ', $selectedValues); error_log( "Routine field_id 347: row " . ($row['excelrow'] ?? $rowIndex) . " generated value: " . $row['data'][$targetColumnIndex] ); } unset($row); error_log("Routine field_id 347 completed."); }