0

I tried many ways to eliminate duplicate of a value within an array but I could not delete the value and duplicates.

Array
(
[0] => Array
    (
        [rut] => 333333333
        [inscripcion_id] => 3940
    )

[1] => Array
    (
        [rut] => 444444444
        [inscripcion_id] => 3941
    )

[2] => Array
    (
        [rut] => 333333333
        [inscripcion_id] => 3985
    )

 )

What I would like to get is that only remain in this case:

Array
(
   [rut] => 444444444
   [inscripcion_id] => 3941
)

And others save duplicate values in an array.

NHTorres
  • 1,528
  • 2
  • 21
  • 39
  • And what have you tried to get the expected output? – swidmann Nov 20 '15 at 14:50
  • 2
    This question has been answered before. Take a look at http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php – Fergal Andrews Nov 20 '15 at 14:52
  • @FergalAndrews That did not work for me, probably because it is a multiple arrangement – NHTorres Nov 20 '15 at 14:53
  • @sioesi is this what you are looking for? [Remove duplicate values from multi-dimensional arrays](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – norris-md Nov 20 '15 at 14:55
  • I need to delete the item that has doubled the 'rut' field, code that eliminates identical subarray. – NHTorres Nov 20 '15 at 14:57
  • The referred question is in fact not similar, since only one value in OPs array is present multiple times and the other not. – syck Nov 20 '15 at 14:58
  • In fact, if I remove the 'inscripcion_id' field and remain identical, the code does not serve to eliminate both values, only 1 would eliminate duplication. – NHTorres Nov 20 '15 at 15:00

2 Answers2

0

My solution:

function unique_multidim_array($arreglo){
    $duplicate = array();
    $i = 0;
    foreach ($arreglo as $a) {
      foreach ($a as $b) {
        $count_values[$b]++;
      }
    }
    foreach ($arreglo as $key) {
        if($count_values[$key['rut']] > 1){
            $duplicate[$i] = $key;
            $i++;
        }

    }
    return $duplicate;
}

returns :

Array
(
[0] => Array
    (
        [rut] => 333333333
        [inscripcion_id] => 3940
    )

[1] => Array
    (
        [rut] => 333333333
        [inscripcion_id] => 3985
    )

 )
NHTorres
  • 1,528
  • 2
  • 21
  • 39
0

Here's my try...this require php 5.5.0 through

$arr1 = Array
    (
    "0" => Array
        (
            "rut" => 333333333,
            "inscripcion_id" => 3940
        ),

    "1" => Array
        (
            "rut" => 444444444,
            "inscripcion_id" => 3941
        ),

    "2" => Array
        (
            "rut" => 333333333,
            "inscripcion_id" => 3985
        ),
    "3" => Array
        (
            "rut" => 555555555,
            "inscripcion_id" => 3987
        )

     );

$unique_array = array();
$unique_value = array_keys(array_count_values(array_column($arr1, "rut") ), 1, true);

foreach ($arr1 as $key => $inner_arr) {
    if (in_array($inner_arr["rut"], $unique_value) ) {
        $unique_array[] = $arr1[$key];
        unset($arr1[$key]);
    }
}

output($arr1):

Array
(
    [0] => Array
        (
            [rut] => 333333333
            [inscripcion_id] => 3940
        )

    [2] => Array
        (
            [rut] => 333333333
            [inscripcion_id] => 3985
        )

)

output($unique_array):

Array
(
    [0] => Array
        (
            [rut] => 444444444
            [inscripcion_id] => 3941
        )

    [1] => Array
        (
            [rut] => 555555555
            [inscripcion_id] => 3987
        )

)

Here's a demo

I do have to point out that the original array key will not be in order deal to the unset method

Andrew
  • 2,810
  • 4
  • 18
  • 32