<?php
$array = array ('1' => (object)array(
'id' => 225,
'user_id' => 1,
'name' => 'Blue Quilted Leather Jacket by Minusey - $499'
),
'2' => (object)array(
'id' => 222,
'user_id' => 1,
'name' => 'Darling New Bathtub by Duravit - $6300'
),
'3' => (object)array(
'id' => 222,
'user_id' => 1,
'name' => 'Darling New Bathtub by Duravit - $6300'
)
);
//print_r(array_column((array)$array, 'id'));
function array_unique_by_key($array, $key = 'id') {
$found = array(); // Encountered IDs
$out = array(); // Output array
foreach ($array as $value) {
if (!array_key_exists($key, $value)) throw new Exception('Can\'t find key "' . $key . '"');
$id = $value[$key];
// If already encountered, skip
if (in_array($id, $found)) continue;
// Otherwise, add to found values and to output array
$found[] = $id;
$out[] = $value;
}
return $out;
}
print_r(array_unique_by_key($array));
preferences:
32.16 ms | 402 KiB | 5 Q