remove duplicate values in multidimensional array

2

I have a huge multidimensional array and I need to remove duplicate values with PHP or some function that does this when using CakePHP. Array structure:

Array
(
[0] => Array
    (
        [Advertencia] => Array
            (
                [id] => 
                [tipo_punicao] => CA
                [suspensao_data_inicio] => 
                [funcionario_matricula] => 
                [advertente_matricula] => 
                [recebedor_RH_matricula] => 
            )

        [Funcionario] => Array
            (
                [id] => 25
                [matricula] => 5444
                [nome] => ANDRE
                [quantidade] => 
            )

    )

[1] => Array
    (
        [Advertencia] => Array
            (
                [id] => 
                [tipo_punicao] => A
                [suspensao_data_inicio] => 
                [funcionario_matricula] => 
                [advertente_matricula] => 
                [recebedor_RH_matricula] => 
            )

        [Funcionario] => Array
            (
                [id] => 20
                [matricula] => 5555
                [nome] => JOAO 
                [quantidade] => 
            )

    )

[2] => Array
    (
        [Advertencia] => Array
            (
                [id] => 
                [tipo_punicao] => RC
                [suspensao_data_inicio] => 
                [funcionario_matricula] => 
                [advertente_matricula] => 
                [recebedor_RH_matricula] => 
            )

        [Funcionario] => Array
            (
                [id] => 20
                [matricula] => 5555
                [nome] => JOAO
                [quantidade] => 
            )

    )
)

Notice that two of these data in the array have the same information, that is, [1] and [2] - where nome , matricula and id are equal. I would like one of these data to be removed. It does not matter which one will stay. It would look like this, for example:

Array
(
[0] => Array
    (
        [Advertencia] => Array
            (
                [id] => 
                [tipo_punicao] => CA
                [suspensao_data_inicio] => 
                [funcionario_matricula] => 
                [advertente_matricula] => 
                [recebedor_RH_matricula] => 
            )

        [Funcionario] => Array
            (
                [id] => 25
                [matricula] => 5444
                [nome] => ANDRE
                [quantidade] => 
            )

    )

[1] => Array
    (
        [Advertencia] => Array
            (
                [id] => 
                [tipo_punicao] => A
                [suspensao_data_inicio] => 
                [funcionario_matricula] => 
                [advertente_matricula] => 
                [recebedor_RH_matricula] => 
            )

        [Funcionario] => Array
            (
                [id] => 20
                [matricula] => 5555
                [nome] => JOAO 
                [quantidade] => 
            )

    )

)

Is it possible?

    
asked by anonymous 07.05.2015 / 21:30

1 answer

3

Yes, it is possible. One way to do this is to use the array_unique function, which aims to return a new array with no duplicate values. Here's an example:

$dupArr = array("A" => "foo", 
                "B" => "baz", 
                "C" => "bar", 
                "D" => "foo"); // Valor repetido
$unique = array_unique($dupArr); 

print_r($unique); // [A] => foo [B] => baz [C] => bar

Exemplo

Update

Because it is a multidimensional array , array_unique will not work. You will need to implement a function that checks whether a value exists in the array , so there is a in_array ", but it does not work in multidimensional arrays, so you will need to use it in a recursive :

function in_array_recursive($agulha, $palheiro) {
    foreach ($palheiro as $item) {
        if (($item == $agulha) || (is_array($item) && in_array_recursive($agulha, $item)))
            return true;
    }
    return false;
}

Now you need to scan the array with the foreach and apply the function in_array_recursive , to do this, use the function below:

function uniqueArray($raiz){
    $unique = [];
    foreach ($raiz as $nodo => $nodos){
        foreach ($nodos as $item => $items){
            foreach ($items as $chave => $valor){
                if (!in_array_recursive($valor, $unique) or empty($valor)){
                    $unique[$nodo][$item][$chave] = $valor;
                } else {
                    unset($unique[$nodo]);
                    break 3;
                }   
            }
        }
    }
    return $unique;
}

Use the function as follows:

$dupArray = array(.....);
$unique = uniqueArray($dupArray);

echo "<pre>". print_r($unique, 1). "<pre>";

Exemplo

    
07.05.2015 / 22:09