I have an array and would like to remove duplicates. I'm using php. How to remove duplicate rows (by id).
My array looks like:
Array
(
[0] => Array
(
[id] => 415
)
[1] => Array
(
[id] => 425
)
[2] => Array
(
[id] => 425
)
[3] => Array
(
[id] => 426
)
)
Assuming that id
is the only element of the array, you can walk through the array using serialize
and array_unique
, as array_unique
by itself doesn't work with multidimensional arrays.
$foo = array_map('unserialize', array_unique(array_map("serialize", $foo)));
If you have other elements, @Ghost's answer is probably better
This will also flatten the array to a 1-dimensional array
$data = array(
array(
'id' => 415,
),
array(
'id' => 425,
),
array(
'id' => 425,
),
array(
'id' => 426,
),
);
$data = array_unique(
array_map('end', $data)
);
var_dump($data);
gives
Array
(
[0] => 415
[1] => 425
[3] => 426
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.