Current Location: Home> Latest Articles> Use array_diff() and array_column() to handle two-dimensional data differences

Use array_diff() and array_column() to handle two-dimensional data differences

M66 2025-05-17

In PHP programming, when processing two-dimensional arrays, you often encounter situations where you need to find the difference between two arrays. Fortunately, PHP provides powerful built-in functions array_diff() and array_column() , which can help us handle array differences easily. This article will describe how to use these two functions to efficiently handle data differences in two-dimensional arrays.

1. Introduction to array_diff() function

The array_diff() function is used to compare elements of two or more arrays and return a new array containing differential elements. This function deletes elements that exist in other arrays, retaining only elements that are unique to the first array.

grammar:

 array_diff(array $array1, array ...$arrays): array
  • $array1 : The first array.

  • $arrays : One or more arrays for comparison.

2. Introduction to array_column() function

The array_column() function is used to extract the data of a column from a two-dimensional array and return an array containing all the values ​​of the column. This is very useful when dealing with complex two-dimensional arrays, especially when you only need to process a column of data in the array.

grammar:

 array_column(array $array, mixed $column_key, mixed $index_key = null): array
  • $array : The input 2D array.

  • $column_key : The key of the column to be extracted.

  • $index_key : Optional, key to provide a custom index for the result array.

3. Example: How to compare the difference between a 2D array by array_diff() and array_column()

Suppose we have two two-dimensional arrays containing user information, we need to find out the user data in array A and not in array B.

 <?php
// ArrayA:User data(Include name、Age and email)
$arrayA = [
    ['name' => 'Alice', 'age' => 25, 'email' => 'alice@example.com'],
    ['name' => 'Bob', 'age' => 30, 'email' => 'bob@example.com'],
    ['name' => 'Charlie', 'age' => 35, 'email' => 'charlie@example.com'],
];

// ArrayB:已有User data
$arrayB = [
    ['name' => 'Alice', 'age' => 25, 'email' => 'alice@example.com'],
    ['name' => 'David', 'age' => 40, 'email' => 'david@example.com'],
];

// 提取ArrayA和ArrayBIn-houseemailList
$emailsA = array_column($arrayA, 'email');
$emailsB = array_column($arrayB, 'email');

// usearray_diff()找出在ArrayAmiddle但不在ArrayBIn-houseemail
$diffEmails = array_diff($emailsA, $emailsB);

// Get the difference data(在ArrayAmiddle但不在ArrayBmiddle)
$diffData = [];
foreach ($arrayA as $user) {
    if (in_array($user['email'], $diffEmails)) {
        $diffData[] = $user;
    }
}

// Output differential data
echo "<pre>";
print_r($diffData);
echo "</pre>";
?>

4. Code explanation

  1. Extract the Email column: We use array_column() to extract the email addresses of all users from a two-dimensional array, generating two arrays $emailsA and $emailsB containing all email addresses.

  2. Find the difference: Then, use the array_diff() function to compare the two email arrays to find the email addresses in $emailsA but not in $emailsB . The returned $diffEmails array contains all the differential emails.

  3. Get the difference data: Finally, we traverse $arrayA to get the user's complete information based on the difference's email address. We check whether each user's email is in $diffEmails through the in_array() function, and if so, add that user information to the $diffData array.

  4. Output result: The result array $diffData contains all user information in array A but not in array B.

5. Output result

According to the above code, the output will be:

 Array
(
    [0] => Array
        (
            [name] => Bob
            [age] => 30
            [email] => bob@example.com
        )
    [1] => Array
        (
            [name] => Charlie
            [age] => 35
            [email] => charlie@example.com
        )
)

As shown above, Bob and Charlie are users in array A and not in array B.