How can two arrays be compared in PHP to keep only the duplicate entries in a new array?
To compare two arrays and keep only the duplicate entries in a new array in PHP, you can use the array_intersect() function. This function compares the values of two arrays and returns the values that are present in both arrays. By using this function, you can easily create a new array containing only the duplicate entries from the original arrays.
$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$duplicateArray = array_intersect($array1, $array2);
print_r($duplicateArray);
Keywords
Related Questions
- What steps can be taken to troubleshoot and confirm if a CSV file is indeed saved in UTF-8 format using tools like Notepad++ or browser inspection?
- Are there any best practices for handling cross-origin requests securely in PHP?
- How can PHP be utilized to manipulate link descriptions in HTML content retrieved through cURL?