Lazarus99 Posted April 26, 2022 Share Posted April 26, 2022 I have an array in php bobby, kevin, bill, kevin, brian, bobby, bobby I am trying to anonymise these into a new array. 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 Quote Link to comment https://forums.phpfreaks.com/topic/314729-group-matching-and-unique-array-elements-php/ Share on other sites More sharing options...
requinix Posted April 26, 2022 Share Posted April 26, 2022 What code have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/314729-group-matching-and-unique-array-elements-php/#findComment-1595714 Share on other sites More sharing options...
Lazarus99 Posted April 26, 2022 Author Share Posted April 26, 2022 To be honest with you it was a complete mess Quote Link to comment https://forums.phpfreaks.com/topic/314729-group-matching-and-unique-array-elements-php/#findComment-1595720 Share on other sites More sharing options...
Solution Barand Posted April 26, 2022 Solution Share Posted April 26, 2022 Here's one method $bidders = ['bobby', 'kevin', 'bill', 'kevin', 'brian', 'bobby', 'bobby' ]; $uniq = array_unique($bidders); for ($i=1; $i<=count($uniq); $i++) { $anon[] = "Bidder$i"; } $trans = array_combine($uniq, $anon); $anonBidders = array_map( function($v) use ($trans) { return $trans[$v]; }, $bidders ); to test echo '<pre>'; vprintf("Bidders | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s <br>", $bidders); vprintf("anonBidders | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s | %-8s <br>", $anonBidders); giving Bidders | bobby | kevin | bill | kevin | brian | bobby | bobby anonBidders | Bidder1 | Bidder2 | Bidder3 | Bidder2 | Bidder4 | Bidder1 | Bidder1 Quote Link to comment https://forums.phpfreaks.com/topic/314729-group-matching-and-unique-array-elements-php/#findComment-1595721 Share on other sites More sharing options...
Lazarus99 Posted April 26, 2022 Author Share Posted April 26, 2022 Perfect, Thank you very much, that worked great for me. Quote Link to comment https://forums.phpfreaks.com/topic/314729-group-matching-and-unique-array-elements-php/#findComment-1595728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.