[PHP] removing all duplicate values from an array
Hi, I'm looking for a function that does almost the same as array_unique() But it must also delete the other duplicate entry. sample to explain: array( 'a' => 'one', 'b' => 'one', 'c' => 'zero', 'd' => 'two' ); I now want the result to be: array( 'c' => 'zero', 'd' => 'two' ); This way I will be able to do an array_diff and then see all the duplicate entries listed. this is to find dup. ip address for a hosting system, displaying both accounts will make it easy to trace the amount of accounts 1 ip curruntly has. Anybody know how to do this? Thanks, Allowee -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] removing all duplicate values from an array
On Thursday 30 October 2003 20:37, Mike Migurski wrote: > >>I'm looking for a function that does almost the same as array_unique() > >> > >>But it must also delete the other duplicate entry. > > > > > > > >Untested pseudo-PHP follows - > > > > $encountered_elements = array(); > > foreach($original_array as $key => $val) > > if(in_array($val, $encountered_elements)) { > > unset($original_array[$key]); > > } else { > > $encountered_elements[$key] = $val; > > } > > Oops, that should have been: > >$encountered_elements = array(); >foreach($original_array as $key => $val) >if(in_array($val, $encountered_elements)) { >unset($original_array[$encountered_elements[$val]]); >} else { >$encountered_elements[$val] = $key; >} Hi, I edited it a bit to match my script and after some tweaks of my own code it started working :) Thanks, Allowee > > > - > michal migurski- contact info and pgp key: > sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php