Answer the question
In order to leave comments, you need to log in
How to remove duplicate elements in a multidimensional array?
Hello. I have the following problem. I have an array like this:
Array
(
[0] => Array
(
[id_city] => 2096
[ip_start] => 2.60.0.0
[ip_end] => 2.60.255.255
[city] => Омск
)
[1] => Array
(
[id_city] => 676
[ip_start] => 2.61.0.0
[ip_end] => 2.61.255.255
[city] => Абакан
)
[2] => Array
(
[id_city] => 1706
[ip_start] => 2.62.0.0
[ip_end] => 2.62.255.255
[city] => Кемерово
)
[3] => Array
(
[id_city] => 1428
[ip_start] => 2.63.0.0
[ip_end] => 2.63.127.255
[city] => Красноярск
)
...
array_unique()
it won't work here. Can you please tell me how to remove all repetitions and leave only unique array elements in the city field? Answer the question
In order to leave comments, you need to log in
// получаем уникальные ключи
$uniqueKeys = array_keys(
array_unique(
array_map(function($item) {
return $item['city'];
}, $array)
)
);
// извлекаем в новый массив те ключи, которые были уникальными
$uniqueArray = array_filter($array, function($itemKey) {
return in_array($itemKey, $uniqueKeys);
}, ARRAY_FILTER_USE_KEY);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question