$type === 'input'); $masterCache = []; // 2. Extract ALL possible unique IDs dynamically from your JSON configurations $max_runs = 1; // Default fallback to at least 1 run foreach ($noten_config as $n_id => $data) { $config = json_decode($data['anzahl_laeufe_json'], true); foreach ($config as $geraetKey => $value) { if ($geraetKey === 'default') { $max_runs = max($max_runs, (int)$value); continue; } if (is_array($value)) { foreach ($value as $progKey => $runsCount) { $max_runs = max($max_runs, (int)$runsCount); } } } } // Ensure we have array lists instead of associative maps $all_programms = array_values($all_programms); $all_geraete = array_values($all_geraete); // Generate standard run sequences (e.g., if max_runs is 2, creates [1, 2]) $all_runs = range(1, $max_runs); $masterCache = []; // 3. The Grand Loop foreach ($all_programms as $programm_id) { foreach ($all_geraete as $geraetId) { foreach ($all_runs as $initNoteRun) { $all_needed_noten = []; foreach ($noten_rank_live_geraet as $ind => $s_note) { $n_id = $s_note['n_id']; $anzahl_laeufe_json = $noten_config[$n_id]['anzahl_laeufe_json'] ?? ''; $anzahl_laeufe = json_decode($anzahl_laeufe_json, true); $g_id = ($s_note['g_id'] === 'A') ? (int) $geraetId : (int) $s_note['g_id']; $run = (int) $s_note['r']; $runs = $anzahl_laeufe[$g_id][(int) $programm_id] ?? $anzahl_laeufe[$g_id]['all'] ?? $anzahl_laeufe['default'] ?? 0; if (((int) $runs) < $run) { unset($noten_rank_live_geraet[$ind]); continue; } $key = "$n_id|$g_id|$run"; if (!isset($all_needed_noten[$key])) { $all_needed_noten[$key] = [ "r" => $run, "g_id" => $g_id, "n_id" => $n_id ]; } } foreach ($noten_rank_live_overview as $s_note) { $n_id = $s_note['n_id']; $g_id = $s_note['g_id']; $run = $s_note['r']; $key = "$n_id|$g_id|$run"; if (!isset($all_needed_noten[$key])) { $all_needed_noten[$key] = [ "r" => $run, "g_id" => $g_id, "n_id" => $n_id ]; } } $all_updating_noten = []; foreach ($following_config_array as $this_note_id => $s_note_abhaenige_rechnungen_json) { $s_note_abhaenige_rechnungen = json_decode($s_note_abhaenige_rechnungen_json ?? '') ?? []; foreach ($s_note_abhaenige_rechnungen as $s_rechnung) { $this_note_geraet_id = $s_rechnung[1]; if ($this_note_geraet_id !== 'A' && (int) $this_note_geraet_id !== (int) $geraetId) { continue; } $target_note_id = $s_rechnung[0]; $target_run_raw = $s_rechnung[2][0] ?? 'A'; $target_geraet_id_raw = $s_rechnung[2][1] ?? 'A'; $target_run = ($target_run_raw === 'A') ? $initNoteRun : (int) $target_run_raw; $target_geraet_id = ($target_geraet_id_raw === 'A') ? $geraetId : (int) $target_geraet_id_raw; $key = "$target_note_id|$target_geraet_id|$target_run"; if (!isset($all_updating_noten[$key])) { $all_updating_noten[$key] = [ "r" => $target_run, "g_id" => $target_geraet_id, "n_id" => $target_note_id ]; } } } foreach ($all_inputs_noten_ids as $n_id => $_) { $anzahl_laeufe_json = $noten_config[$n_id]['anzahl_laeufe_json'] ?? ''; $anzahl_laeufe = json_decode($anzahl_laeufe_json, true); $runs = $anzahl_laeufe[$geraetId][(int) $programm_id] ?? $anzahl_laeufe[$geraetId]['all'] ?? $anzahl_laeufe['default'] ?? 0; if ($initNoteRun > $runs) { continue; } $key = "$n_id|$geraetId|$initNoteRun"; if (!isset($all_updating_noten[$key])) { $all_updating_noten[$key] = [ "r" => $initNoteRun, "g_id" => $geraetId, "n_id" => $n_id ]; } } $intersect_noten = array_intersect_key($all_updating_noten, $all_needed_noten); $key = "$rang_note_id|0|1"; if (!isset($all_updating_noten[$key])) { $all_updating_noten[$key] = [ "r" => 1, "g_id" => 0, "n_id" => $rang_note_id ]; } $sql_where_array = []; $sql_where_values = []; $i = 1; foreach ($intersect_noten as $s_note) { $sql_where_values[] = $s_note['n_id']; $sql_where_values[] = $s_note['g_id']; $sql_where_values[] = $s_note['r']; $sql_where_array[] = '(`note_bezeichnung_id` = ? AND `geraet_id` = ? AND `run_number` = ?)'; } $sql_where_string = ''; if (!empty($sql_where_values)) { $sql_where_string = implode(' OR ', $sql_where_array); } if (!empty($intersect_noten)) { $masterCache[$programm_id][$geraetId][$initNoteRun] = ["SQL" => $sql_where_string, "values" => $sql_where_values, "intersect_noten" => $intersect_noten]; } } } } // 4. Save to file $fileContent = "