query( << $query['periodo_id']] ); switch ($query['query']) { case 'cursos': // Get all courses from the database $courses = array_column($moodle_db ->where('visible', 1) ->get(tableName: 'mdl_course', columns: ['shortname']), 'shortname'); // Check if the shortname exists in the courses array (case-insensitive) $data = array_map(function ($row) use ($courses) { $shortname = $row['Materia_shortname']; $row['Moodle'] = in_array(strtolower($shortname), array_map('strtolower', $courses)) ? 'Existe' : 'No existe'; return $row; }, $data); break; } $filename = 'test.csv'; header('Content-Type: text/csv; charset=UTF-8'); header('Content-Disposition: attachment; filename="' . $filename . '"'); echo "\xEF\xBB\xBF"; // Añade el BOM de UTF-8 al inicio del archivo para indicar su codificación $fp = fopen('php://output', 'w'); // 'wb' también es válido en este contexto // insert header $headers = array_keys($data[0]); // todos los headers en Mayúsculas incluyendo los acentos fputcsv($fp, $headers); // insert data foreach ($data as $line) { fputcsv($fp, $line); } fclose($fp); exit();