JBZoo\Utils\Arr::unique PHP Method

unique() public static method

Remove the duplicates from an array.
public static unique ( array $array, boolean $keepKeys = false ) : array
$array array
$keepKeys boolean
return array
    public static function unique($array, $keepKeys = false)
    {
        if ($keepKeys) {
            $array = array_unique($array);
        } else {
            // This is faster version than the builtin array_unique().
            // http://stackoverflow.com/questions/8321620/array-unique-vs-array-flip
            // http://php.net/manual/en/function.array-unique.php
            $array = array_keys(array_flip($array));
        }
        return $array;
    }

Usage Example

Example #1
0
 public function testUnique()
 {
     $array = array(10, 100, 1231, 10, 600, 20, 40, 1231, 20, 6, 1);
     isSame(array(10, 100, 1231, 600, 20, 40, 6, 1), Arr::unique($array));
     $array = array('hello', 'world', 'this', 'is', 'a', 'test', 'hello', 'is', 'a', 'word');
     isSame(array('hello', 'world', 'this', 'is', 'a', 'test', 'word'), Arr::unique($array, false));
     $array = array('asd_1' => 'asd', 'asd_2' => 'asd');
     isSame(array('asd_1' => 'asd'), Arr::unique($array, true));
 }
All Usage Examples Of JBZoo\Utils\Arr::unique