Get values which only occur in one of two flat arrays

Get values which only occur in one of two flat arrays

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Finding Unique Elements Between Two Flat Arrays

When working with data sets, you often need to identify items that exist exclusively in one collection versus another. Consider a scenario where $array1 contains [64, 98, 112, 92, 92, 92] and $array2 holds a longer sequence including 3, 26, 38, up to 112. The goal is to isolate the values present in one array that do not appear in the other. This operation is technically known as finding the symmetric difference or, more specifically here, the set difference depending on which direction you are looking.

The most straightforward approach leverages PHP's built-in array functions designed for set operations. If you want elements from $array1 that are absent from $array2, array_diff() is the standard tool. Conversely, if you need elements from $array2 missing from $array1, you simply swap the arguments.

$onlyInArray1 = array_diff($array1, $array2); // [ ] -> Empty in this specific case
$onlyInArray2 = array_diff($array2, $array1); // [3, 26, 38, ... 94, 96, 100, 104, 106, 108, 110]

However, looking at the provided data, there is a critical nuance often missed: duplicates and order preservation. The array_diff() function treats arrays as sets regarding values but preserves the index structure of the result array based on the first argument. More importantly, it does not perform a symmetric difference by default; it strictly subtracts the second array from the first.

In the specific example provided, $array1 contains 64 and 98 and 112 and 92. Notice that $array2 also contains 64, 98, and 112. It also contains 92. Because every single value in $array1 exists somewhere within $array2, the result of array_diff($array1, $array2) is an empty array. The values 92 appear three times in $array1 but only once in $array2. Since array_diff() checks for existence rather than count matching, it ignores the extra occurrences in the first array as long as at least one match exists in the second.

If your requirement shifts to finding values that occur only once across the union of both arrays (ignoring which specific array they came from), the logic changes entirely. You would need to flatten the combined data, count occurrences using array_count_values(), and then filter for those with a count of one. This is distinct from "values unique to one array." For instance, if you had [1, 2] in array A and [2, 3] in array B, the value 2 is shared. The values 1 and 3 are unique to their respective arrays.

Here is how you might calculate the symmetric difference manually if you needed a single list of all unique items regardless of origin:

$diff = array_values(array_diff($array1, $array2) + array_diff($array2, $array1));
// This ensures we get a re-indexed numeric array containing only the asymmetric elements.

When dealing with large datasets, performance becomes a consideration. array_diff() iterates through both arrays. In older PHP versions or specific contexts involving multidimensional arrays, this could be slower than expected, but for flat integer arrays as shown in your source material, it is highly optimized and efficient.

If you are working within the Laravel ecosystem, you might find yourself handling this logic in a service layer or a controller. While native PHP functions handle this perfectly well for simple arrays, Laravel provides helper methods in collections that can make the intent more readable if you are converting your arrays to Collection objects first. You could chain methods like diff() on a Collection instance, which adds method chaining capabilities but incurs the memory overhead of wrapping the array in an object structure. For raw performance with simple integer lists, sticking to the native array_diff() remains the best engineering choice unless you need the additional collection features elsewhere in your codebase.

Always verify whether "difference" means strictly values missing from the second set or a symmetric difference where you want items missing from either side. The source material implies finding what is different between them, which usually defaults to array_diff($array1, $array2), but understanding the directionality prevents logic errors in downstream processing.

Our network