3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $array = array( 'foo' => 2, 'baz' => 3, 'bar' => 4, 'jim' => 2, ); function unique($input) { $output = array(); foreach ($input as $key => $val) { if (!in_array($val, $output)) { $output[$key] = $val; } } return $output; } function unique_better($input) { $output = array(); $unique_values = array(); foreach ($input as $key => $val) { if ( ! isset( $unique_values[$val] ) ) { $unique_values[$val] = true; $output[$key] = $val; } } return $output; } var_dump(unique($array)); var_dump(unique_better($array)); var_dump(array_flip(array_flip($array)));
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.13.2, 7.0.0 - 7.1.0
array(3) { ["foo"]=> int(2) ["baz"]=> int(3) ["bar"]=> int(4) } array(3) { ["foo"]=> int(2) ["baz"]=> int(3) ["bar"]=> int(4) } array(3) { ["jim"]=> int(2) ["baz"]=> int(3) ["bar"]=> int(4) }