remove duplicate values in multidimensional array

Asked

Viewed 4,783 times

2

I have a huge multidimensional array and 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] => 
            )

    )
)

Note that two of these array data have the same information, ie [1] and [2] - where the nome, matricula and id are equal. I would like one of these data to be removed. It does not matter which of the two will remain. 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] => 
            )

    )

)

It is possible?

1 answer

3


It is possible yes. One of the ways to do this is to use the function array_unique, whose purpose is to return a new array no duplicate values. See 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

Updating

Because it is a array multidimensional, the array_unique will not work. It will be necessary to implement a function that checks whether a value exists in the array, for this, there is the function in_array, but it does not work in arrays multidimensional, therefore, it will be necessary 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 it will be necessary to sweep 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

  • It looks like good code, I did exactly this and the returning array comes empty. ;(

  • It seems that your code works perfectly. I believe that in mine it did not work for some inconsistency of the array. Anyway I already found a way this array does not come by default with duplicate values. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.