对于php >=7.0
:
由于array_column
自 PHP 7.0 起适用于对象数组,您可以按照@plashenkov 的建议使用以下组合:
$filtered = array_intersect_key($array, array_unique(array_column($array, 'someProperty')));
完整示例:https ://3v4l.org/IboLu#v8.0.8
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 */
$filtered = array_intersect_key($array, array_unique(array_column($array, 'name')));
/** Answer Code ends here */
echo 'After removing duplicates\n';
print_r($filtered);
输出:
Original array:
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)
[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)
[2] => my_obj Object
(
[term_id] => 15
[name] => Campaign Finance
[slug] => campaign-finance-good-government-political-reform
)
)
After removing duplicates
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)
[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)
)
term_id 为 15 的对象已被删除,因为它与 term_id 14 同名。
对于php <7.0
:
使用现有键和名称作为值构建一个新数组,使用array_unique
(注意它保留键)。
然后将原始数组中的每个键复制到一个新数组 ( $filtered
) 中(或从原始键中删除所有不在唯一数组中的内容)。
编辑:完整示例:https ://3v4l.org/SCrko#v5.6.40
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);
输出:
Original array:
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)
[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)
[2] => my_obj Object
(
[term_id] => 15
[name] => Campaign Finance
[slug] => campaign-finance-good-government-political-reform
)
)
After removing duplicates
Array
(
[0] => my_obj Object
(
[term_id] => 23
[name] => Assasination
[slug] => assasination
)
[1] => my_obj Object
(
[term_id] => 14
[name] => Campaign Finance
[slug] => campaign-finance
)
)
term_id 为 15 的对象已被删除,因为它与 term_id 14 同名。