Current Location: Home> Latest Articles> How to Extract Keys Using array_keys() and Perform Refined Comparison with array_diff_uassoc()

How to Extract Keys Using array_keys() and Perform Refined Comparison with array_diff_uassoc()

M66 2025-06-24

In PHP, arrays are a powerful data structure that allows us to store and process large amounts of data. Sometimes, we need to extract keys from an array or compare two arrays while ensuring that the comparison process considers both the keys and values. Today, we will introduce how to combine array_keys() and array_diff_uassoc() to achieve this goal.

1. Extract Keys from an Array Using array_keys()

array_keys() function allows you to extract all the keys from an array and return them as an indexed array. This function is very useful, especially when you need to operate on the keys of an array. Here’s the basic usage of array_keys():

<?php
$arr = [
    'apple' => 1,
    'banana' => 2,
    'cherry' => 3
];
<p>$keys = array_keys($arr);<br>
print_r($keys);<br>
?><br>

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

In this example, array_keys() returns an array containing the keys of the original array. You can use these keys for further operations as needed.

2. Perform Refined Comparison with array_diff_uassoc()

array_diff_uassoc() function compares two arrays based on both their keys and values, allowing for a refined comparison using a custom comparison function. This function is similar to array_diff_assoc(), but it allows you to define your own comparison rules. It returns an array containing the differences.

array_diff_uassoc() has the following basic syntax:

array_diff_uassoc(array $array1, array $array2, callable $value_comparator)
  • $array1 and $array2 are the arrays to compare.

  • $value_comparator is the custom comparison function used to compare the values in the arrays.

Let’s look at an example where we combine array_keys() and array_diff_uassoc() to perform a refined comparison:

<?php
// Define two arrays
$array1 = [
    'apple' => 1,
    'banana' => 2,
    'cherry' => 3
];
<p>$array2 = [<br>
'apple' => 1,<br>
'banana' => 3,<br>
'cherry' => 3<br>
];</p>
<p>// Custom comparison function: compare if two values are equal<br>
function custom_compare($value1, $value2) {<br>
return $value1 - $value2;<br>
}</p>
<p>// Use array_diff_uassoc for refined comparison<br>
$result = array_diff_uassoc($array1, $array2, 'custom_compare');<br>
print_r($result);<br>
?><br>

Output:

Array
(
    [banana] => 2
)

In this example, we defined two arrays, $array1 and $array2, and used the custom_compare function to compare their values. array_diff_uassoc() returns an array containing the difference, which is the banana key. The value of banana is 2 in $array1 and 3 in $array2, so banana is included in the difference result.

3. Combining array_keys() and array_diff_uassoc() for Key Difference Comparison

In practical use, we may only be concerned with specific keys and want to extract those keys using array_keys(), then perform a refined comparison using array_diff_uassoc(). For example, let’s say we only want to compare the apple and cherry keys:

<?php
// Define two arrays
$array1 = [
    'apple' => 1,
    'banana' => 2,
    'cherry' => 3
];
<p>$array2 = [<br>
'apple' => 1,<br>
'banana' => 3,<br>
'cherry' => 4<br>
];</p>
<p>// Get the keys to compare<br>
$keys_to_compare = array_keys($array1); // ['apple', 'banana', 'cherry']</p>
<p>// Custom comparison function: compare if two values are equal<br>
function custom_compare($value1, $value2) {<br>
return $value1 - $value2;<br>
}</p>
<p>// Compare and return the differences<br>
$result = array_diff_uassoc(<br>
array_intersect_key($array1, array_flip($keys_to_compare)),<br>
array_intersect_key($array2, array_flip($keys_to_compare)),<br>
'custom_compare'<br>
);</p>
<p>print_r($result);<br>
?><br>

Output:

Array
(
    [cherry] => 3
)

In this example, we first extracted all the keys from $array1 using array_keys(). Then, we used array_intersect_key() to keep only the keys we want to compare (in this case, apple and cherry). Finally, we used array_diff_uassoc() for refined comparison, and the result shows that the value of cherry in $array1 is 3, while in $array2 it is 4, so cherry is returned as the difference.

Conclusion

By combining array_keys() and array_diff_uassoc(), we can perform more flexible and refined comparisons on the keys and values within arrays. array_keys() makes it easy to extract the keys, while array_diff_uassoc() allows us to customize the comparison rules, ensuring that both the keys and values are considered during the comparison. Mastering these two functions can greatly enhance our ability to work with arrays in PHP.