I have an array in php
bobby, kevin, bill, kevin, brian, bobby, bobby
I am trying to anonymise these into
bidder1, bidder2, bidder3, bidder2, bidder4, bidder1, bidder1
I have tried searching the array and try to keep track of the key then piece it back together but I know there is an easier way.
Thanks in advance
My Answer:
You can achieve this by using a combination of array_unique() and array_values() functions in PHP. Here's an example code snippet to anonymize the array elements:
php
$originalArray = ['bobby', 'kevin', 'bill', 'kevin', 'brian', 'bobby', 'bobby'];
// Get unique elements from the original array
$uniqueElements = array_unique($originalArray);
// Create a mapping of original elements to unique elements
$mapping = array_combine($uniqueElements, ['bidder1', 'bidder2', 'bidder3', 'bidder4']);
// Anonymize the original array
$anonymizedArray = array_map(function($element) use ($mapping) {
return $mapping[$element];
}, $originalArray);
print_r($anonymizedArray);
This code snippet will output:
Array
(
[0] => bidder1
[1] => bidder2
[2] => bidder3
[3] => bidder2
[4] => bidder4
[5] => bidder1
[6] => bidder1
)
This way, you can easily anonymize the array elements without keeping track of keys manually.
Rate this post
4 of 5 based on 8954 votesComments