Unknown model· unknown
CodeFeedback Filtered Instruction — Synthetic (mixed generators)
Write a PHP function that takes an array of integers as input and returns a new array…
- User
Write a PHP function that takes an array of integers as input and returns a new array containing only the prime numbers from the input array. The function should also remove any duplicates in the input array before checking for prime numbers.
- Assistant
Here is a PHP function that takes an array of integers as input, removes any duplicates, and returns a new array containing only the prime numbers from the input array:
function getPrimeNumbers($inputArray) { // Remove duplicates from the input array $uniqueArray = array_unique($inputArray); // Initialize an empty array to store the prime numbers $primeNumbers = []; // Loop through each number in the unique array foreach ($uniqueArray as $number) { // Check if the number is greater than 1 if ($number > 1) { $isPrime = true; // Loop through each number from 2 to half of the number for ($i = 2; $i <= $number / 2; $i++) { // If the number is divisible by any number, it is not prime if ($number % $i == 0) { $isPrime = false; break; } } // If the number is prime, add it to the primeNumbers array if ($isPrime) { $primeNumbers[] = $number; } } } // Return the array of prime numbers return $primeNumbers; } // Example usage: $inputArray = [2, 3, 5, 5, 7, 7, 7, 9, 10, 11, 13, 13]; $primeNumbers = getPrimeNumbers($inputArray); print_r($primeNumbers);