3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Remove duplicate elements from an array using a user-defined Reductor function. * @param array $array * @param callable $reductor Reduces a single array element to a simple type for equivalence checking. */ function array_uunique(array $array, callable $reductor) { $seen = []; return array_filter( $array, function($a)use(&$seen, $reductor){ $val = $reductor($a); if( ! in_array($val, $seen, true) ) { $seen[] = $val; return true; } else { return false; } } ); } $arr = [ [ 'target' => 'a' ], [ 'target' => 'b' ], [ 'target' => 'c' ], [ 'target' => 'd' ], [ 'target' => 'c' ], [ 'target' => 'e' ], ]; echo json_encode( array_uunique($arr, function($a){return $a['target'];}), JSON_PRETTY_PRINT ) . PHP_EOL;
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.17.3, 7.0.0 - 7.2.6
{ "0": { "target": "a" }, "1": { "target": "b" }, "2": { "target": "c" }, "3": { "target": "d" }, "5": { "target": "e" } }