In PHP, array_combine and array_unique are two very practical functions. They can play a big role when we need to build unique key-value pairs. This article will explain in detail how to use these two functions to construct an array that contains only unique key-value pairs.
In actual development, we often encounter scenarios where we need to process arrays, especially when we have two arrays, one as keys and the other as values, how to generate unique key-value pairs based on these data?
array_combine($keys, $values) : This function will accept two arrays, $keys as the key of the new array and $values as the value of the new array. If the number of elements of the two arrays is not equal, array_combine will return false .
array_unique($array) : This function is used to remove duplicate values in an array and only unique values are retained.
What we are going to discuss today is how to use these two functions to construct a unique array of key-value pairs.
Suppose we have two arrays, one representing the user ID (used as a key) and the other representing the user's email address (used as a value). We need to make sure that the email address is not duplicated and form an array of key-value pairs with the corresponding user ID.
<?php
// user ID Array
$userIds = [1, 2, 3, 4, 5];
// user邮箱Array
$userEmails = [
'alice@example.com',
'bob@example.com',
'alice@example.com',
'charlie@example.com',
'dave@example.com'
];
// Remove duplicate email addresses
$uniqueEmails = array_unique($userEmails);
// use array_combine Create a unique key-value pair
$uniqueUserMap = array_combine($userIds, $uniqueEmails);
// Output result
print_r($uniqueUserMap);
?>
Original array:
$userIds contains the user's ID.
$userEmails contains the email address corresponding to each user ID, but note that there are duplicate emails in it.
Use of array_unique :
array_unique($userEmails) removes duplicate values in the $userEmails array, ensuring that each email address is unique.
Use of array_combine :
We combine the unique email address with the user ID through array_combine($userIds, $uniqueEmails) to form a new associative array.
Suppose that after the above code is executed, the output result is as follows:
Array
(
[1] => alice@example.com
[2] => bob@example.com
[3] => charlie@example.com
[4] => dave@example.com
)
As you can see, the duplicate email address alice@example.com was removed and we ended up getting key-value pairs for each user ID and unique email address.
Suppose you want to process the URL and need to ensure that the domain names of each URL are unified, we can show how to modify the URL domain names in the array through array_map .
<?php
// original URL Array
$urls = [
'https://example.com/page1',
'https://example.com/page2',
'https://oldsite.com/page3',
'https://newsite.com/page4'
];
// use array_map Modify all URL The domain name is m66.net
$updatedUrls = array_map(function($url) {
return preg_replace('/https?:\/\/[^\/]+/', 'https://m66.net', $url);
}, $urls);
// Output modified URL Array
print_r($updatedUrls);
?>
We first create an array containing multiple URLs.
Through the array_map and preg_replace functions, we replace each URL domain name in the array with m66.net .
Finally, the modified URL array is output to ensure that all domain names are unified.
Array
(
[0] => https://m66.net/page1
[1] => https://m66.net/page2
[2] => https://m66.net/page3
[3] => https://m66.net/page4
)
With array_combine and array_unique , we can easily construct an array containing unique key-value pairs, and with some other functions (such as array_map ), we can further process the data. For example, modify the URL domain name uniformly to ensure that each URL meets the format we need.
Hopefully this article helps you understand how to use these two PHP functions to process array data in actual development. If you have any questions, please feel free to leave a message!