"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Find the Unique Values Between Two Flat Arrays in PHP?

How Can I Find the Unique Values Between Two Flat Arrays in PHP?

Published on 2024-12-23
Browse:816

How Can I Find the Unique Values Between Two Flat Arrays in PHP?

Finding Unique Values Between Flat Arrays

Given two arrays, the task is to determine the values that exist only in one of them. This operation is commonly known as finding the difference between two sets.

In PHP, you can utilize the array_merge, array_diff, and array_diff functions to achieve this. Here's a detailed solution:

$array1 = [64, 98, 112, 92, 92, 92];
$array2 = [3, 26, 38, 40, 44, 46, 48, 52, 64, 68, 70, 72, 102, 104, 106, 92, 94, 96, 98, 100, 108, 110, 112];

// Calculate the difference between the two arrays
$diff1 = array_diff($array1, $array2);
$diff2 = array_diff($array2, $array1);

// Merge the two difference arrays to obtain the unique values
$fullDiff = array_merge($diff1, $diff2);

print_r($fullDiff);

This approach ensures that values present in both arrays are eliminated from the final result, leaving you with an array containing only the unique values that exist in one of the two original arrays.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3