3v4l.org

run code in 300+ PHP versions simultaneously
<?php class my_obj { public $term_id; public $name; public $slug; public function __construct($i, $n, $s) { $this->term_id = $i; $this->name = $n; $this->slug = $s; } } $objA = new my_obj(23, 'Assasination', 'assasination'); $objB = new my_obj(14, 'Campaign Finance', 'campaign-finance'); $objC = new my_obj(15, 'Campaign Finance', 'campaign-finance-good-government-political-reform'); $array = array($objA, $objB, $objC); echo 'Original array:\n'; print_r($array); /** Answer Code begins here **/ // Build temporary array for array_unique $tmp = array(); foreach($array as $k => $v) $tmp[$k] = $v->name; // Find duplicates in temporary array $tmp = array_unique($tmp); // Build new array with only non-duplicate items $filtered = []; foreach($array as $k => $v) { if (array_key_exists($k, $tmp)) $filtered[$k] = $v; } /** Answer Code ends here **/ echo 'After removing duplicates\n'; print_r($filtered);

preferences:
24.05 ms | 405 KiB | 5 Q