0

I have an Array which has 3 Dimensions:

$data_ary[$k_0][$v_0[4]]['...'] = ...

In the brackets with '...' I write Parameters like "Country", "Designation", "Year" and so on. Now as I run this through a loop the values for $k_0 increases every loop by 1 and $v_0[4] always changes the specific value.

My Problem: Very often the values are duplicates across the different dimensions. For Example:

$data_ary[1][1]['Country'] = 'Germany';
$data_ary[1][1]['Year']    = '2017';
$data_ary[2][1]['Country'] = 'Germany';
$data_ary[2][1]['Year']    = '2017';

How do I delete those duplicates? I have tried array_unique() but the result was that from the 27.000 entries 26999 got deleted..

Some Example Input:

$data_ary[6]['GERMANY']['YEAR']      = 2017;
$data_ary[6]['GERMANY']['MONTH']     = 1;
$data_ary[6]['GERMANY']['ID']        = 6010;
$data_ary[6]['GERMANY']['COUNTRY']   = 'GERMANY';

$data_ary[7]['ITALY']['YEAR']        = 2016;
$data_ary[7]['ITALY']['MONTH']       = 4;
$data_ary[7]['ITALY']['ID']          = 52752;
$data_ary[7]['ITALY']['COUNTRY']     = 'ITALY';

$data_ary[8]['GERMANY']['YEAR']      = 2017;
$data_ary[8]['GERMANY']['MONTH']     = 1;
$data_ary[8]['GERMANY']['ID']        = 6010;
$data_ary[8]['GERMANY']['COUNTRY']   = 'GERMANY';

As you can See the first and the second are basically the same but differ by the value in the first bracktes.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
C. Henke
  • 151
  • 1
  • 13
  • please show some example input array for clearence. – Alive to die - Anant Nov 20 '17 at 10:14
  • 1
    i think your array is multidimensional, please check this one https://stackoverflow.com/questions/3598298/php-remove-duplicate-values-from-multidimensional-array – Casper Nov 20 '17 at 10:17
  • @AlivetoDie I added an example. I will look again closer to the other "possible duplicates" but those solutions didnt work for me so far. – C. Henke Nov 20 '17 at 10:23

1 Answers1

0

You can do it like below:-

$input = array_map("unserialize", array_unique(array_map("serialize", $data_ary)));

print_r($input);

Output:- https://eval.in/903355

Reference:- How to remove duplicate values from a multi-dimensional array in PHP

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98