/ Published in: PHP
Filter array to have only unique values
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function arrayUnique($array, $preserveKeys = false) { // Unique Array for return // Array with the md5 hashes foreach($array as $key => $item) { // Serialize the current element and create a md5 hash // If the md5 didn't come up yet, add the element to // to arrayRewrite, otherwise drop it // Save the current element hash $arrayHashes[$hash] = $hash; // Add element to the unique Array if ($preserveKeys) { $arrayRewrite[$key] = $item; } else { $arrayRewrite[] = $item; } } } return $arrayRewrite; }