note 104102 deleted from function.array-unique by crell

From: Date: Tue, 12 Apr 2022 00:47:24 +0000
Subject: note 104102 deleted from function.array-unique by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218159@lists.php.net to get a copy of this message
Note Submitter: 0cool.f ---- Hope this can help... <?php function array_unique_key_group($array) { if(!is_array($array)) return false; $temp = array_unique($array); foreach($array as $key => $val) { $i = array_search($val,$temp); if(!empty($i) && $key != $i) { $temp[$i.','.$key] = $temp[$i]; unset($temp[$i]); } } return $temp; } ?> this function return an array that is unique, but preserve every key for the element... sorry for bad english I'm italian... $array['asd'] = 'value'; $array['lol'] = 'value'; $array['foo'] = 'val'; $array['bar'] = 'val'; var_dump(array_unique_key_group($array)); // will be output array(2) { ["asd,lol"]=> string(5) "value" ["foo,bar"]=> string(3) "val" }

« previous php.notes (#218159) next »