Answer the question
In order to leave comments, you need to log in
How to remove duplicate values in a 2D array?
Hey!
Tell me, please, such a problem. There is an array
Array
(
[0] => Array
(
[ID] => 4887
[IBLOCK_ID] => 302
[NAME] => Производитель
[TEST] => Основное
)
[1] => Array
(
[ID] => 4888
[IBLOCK_ID] => 302
[NAME] => Производство
[TEST] => Основное
)
[2] => Array
(
[ID] => 4889
[IBLOCK_ID] => 302
[NAME] => Частота
[TEST] => Не основные характеристики
)
)
$new_arr = array() ;
$result_res = array() ;
foreach ($arr as $key => $value) {
$new_arr[$key] = $value['TEST'] ;
}
$new_arr = array_unique($new_arr) ;
foreach ($new_arr as $key => $value) {
$new_arrs[$key] = $arr[$key] ;
}
echo "<pre>";
print_r($new_arrs);
echo "</pre>";
[1] => Array
(
[ID] => 4888
[IBLOCK_ID] => 302
[NAME] => Производство
[TEST] => Основное
)
Answer the question
In order to leave comments, you need to log in
You can do this:
//$arr - изначальный массив
$arExistsTest = [];
//здесь используется ссылка (&), чтобы можно было удалять вложенный ключ напрямую
//т.е. чтобы можно было сделать "unset($value['TEST'])",
//а не "unset($arr[$key]['TEST'])"
foreach ($arr as &$value){
$currentTestValue = $value['TEST'];
if (isset($arExistsTest[$currentTestValue]))
unset($value['TEST']);
else
$arExistsTest[$currentTestValue] = true;
}
//На данном этапе из массива $arr были удалены
//все повторяющиеся значения "TEST",
//встретившиеся второй раз и далее
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question