"Department of Emergency Medicine", "Surgery" => "Department of Surgery", "Medicine" => "Department of Medicine", "Women's and Children's Health" => "Department of Women's and Children's Health", "Biomedical Science" => "Department of Biomedical Science", "Population Health" => "Department of Population Health", "Clinical Neurosciences" => "Department of Clinical Neurosciences", ]; // Create an array to hold valid department names $validDepartments = []; $specialDeptEmployees = []; // This array will contain employees with their original data $regularDeptEmployees = []; $processedEmployees = []; $employeesInJsonStaff = []; // Iterate through employees and update department names foreach ($files as &$f) { $departments = array_map('trim', explode(', ', $f['department'])); // Split and trim department names by comma // Update department names based on $customDepartmentNames $departments = array_map(function ($department) use ($customDepartmentNames) { return isset($customDepartmentNames[$department]) ? $customDepartmentNames[$department] : $department; }, $departments); // Add the updated department names back to the employee's record $f['department'] = implode(', ', $departments); } // Create a mapping of names to additional data from jsonStaff $nameToDataMapping = []; foreach ($jsonStaff as $staffMember) { $staffName = $staffMember['email']; $nameToDataMapping[$staffName] = $staffMember; } // Separate employees based on the special department unset($f); foreach ($files as $f) { $nameCheck = $f['email']; $adminOffice = $f['admin_office']; // Check if the data is found in $nameToDataMapping if (isset($nameToDataMapping[$nameCheck]) || $adminOffice === "yes") { $additionalData = $nameToDataMapping[$nameCheck]; // Update $f with additional data if found if (isset($f['override_title']) && !empty($f['override_title'])) { $f['title'] = $f['override_title']; } else { $f['title'] = $additionalData['description']; } if (isset($f['override_phone'])) { $f['phone'] = $f['override_phone']; } else { $f['phone'] = $additionalData['phone']; } if (isset($f['override_name'])) { $f['full_name'] = $f['override_name']; } else { $f['full_name'] = $additionalData['fullname']; } if (isset($f['override_email'])) { $f['email'] = $f['override_email']; }else { $f['email'] = $additionalData['email']; } // Add this employee to the $employeesInJsonStaff array $employeesInJsonStaff[] = $f; } else { } $departments = explode(', ', $f['department']); // Split department names by comma // Check if the employee has already been processed $alreadyProcessed = false; foreach ($departments as $department) { if ($department === $specialDepartment) { $specialDeptEmployees[] = $f; $alreadyProcessed = true; // Mark as already processed } else { if (!in_array($f, $processedEmployees)) { $regularDeptEmployees[] = $f; $processedEmployees[] = $f; } if (!in_array($department, $validDepartments)) { $validDepartments[] = $department; } } } } // Create an array to hold unmatched employees in the "Other" department $otherDeptEmployees = []; // Iterate through $jsonStaff to include unmatched employees foreach ($jsonStaff as $staffMember) { $staffEmail = $staffMember['email']; $foundInProcessed = false; $foundInSpecialDept = false; $foundInOtherDept = false; // Check if the email exists in processedEmployees foreach ($processedEmployees as $employee) { if ($employee['email'] == $staffEmail) { $foundInProcessed = true; break; // No need to continue checking } } // Check if the email exists in specialDeptEmployees foreach ($specialDeptEmployees as $employee) { if ($employee['email'] == $staffEmail) { $foundInSpecialDept = true; break; // No need to continue checking } } // Check if the email exists in otherDeptEmployees foreach ($otherDeptEmployees as $employee) { if ($employee['email'] == $staffEmail) { $foundInOtherDept = true; break; // No need to continue checking } } if (!$foundInProcessed && !$foundInSpecialDept) { // Include the employee in the "Other" department $additionalData = $staffMember; // Update the department name to "Other" //$additionalData['department'] = $additionalData['department']; // Set title based on your requirements $additionalData['title'] = $staffMember['description']; $otherDeptEmployees[] = $additionalData; } } // Sort the valid departments again $validDepartments = array_values(array_unique($validDepartments)); sort($validDepartments); // Filter valid departments to exclude those with no employees in the final output $validDepartmentsWithEmployees = array_filter($validDepartments, function ($department) use ($regularDeptEmployees, $employeesInJsonStaff) { return array_reduce($regularDeptEmployees, function ($carry, $employee) use ($department, $employeesInJsonStaff) { $departments = array_map('trim', explode(', ', $employee['department'])); $pattern = '/^' . preg_quote($department, '/') . '$/i'; $hasEmployee = in_array($department, $departments) || preg_match($pattern, implode(', ', $departments)); $isInJsonStaff = in_array($employee, $employeesInJsonStaff); return $carry || ($hasEmployee && $isInJsonStaff); }, false); }); // Sort employees within the special department using custom sorting usort($specialDeptEmployees, function($a, $b) use ($specialDepartment) { // Check if both employees have an order value for the current department $orderA = isset($a['order'][$specialDepartment]) ? $a['order'][$specialDepartment] : null; $orderB = isset($b['order'][$specialDepartment]) ? $b['order'][$specialDepartment] : null; // If both have order, compare by order if ($orderA !== null && $orderB !== null) { return $orderA - $orderB; } // If only one has order, place that one first if ($orderA !== null) { return -1; } if ($orderB !== null) { return 1; } // If neither has an order, fall back to title sorting return strcmp($a['title'], $b['title']); }); // Create select to populate valid department names echo '
'; // Display employees from the special department first echo "Back to menu"; echo "