Remove duplicate array element

Asked

Viewed 572 times

1

I have the following array:

Array
(
 [0] => Array
    (
        [forn_nome_fantasia] =>   FORNECEDOR TESTE
        [class_nvl4_descricao] =>   DESPESAS COM FRETES
    )

 [1] => Array
    (
        [forn_nome_fantasia] => DJ/ DESEO -SALA DE ESTAR 
        [class_nvl4_descricao] =>   DESPESAS COM FRETES

    )
)

Note that the values in the key class_nvl4_descricao are equal. I want to remove this key when it is repeated. Preferably remove all and leave only one.

I wanted it to stay that way:

Array
(
 [0] => Array
   (
    [forn_nome_fantasia] =>   FORNECEDOR TESTE
    [class_nvl4_descricao] =>   DESPESAS COM FRETES
   )

 [1] => Array
   (
    [forn_nome_fantasia] => DJ/ DESEO -SALA DE ESTAR 
    [class_nvl4_descricao] =>   ''

  )
)
  • Put an example of the result to make it easier to understand your problem.

  • You want to check only this key "class_nvl4_description" or the other key "forn_name_fantasy"?

  • Only the key class_nvl4_descricao

2 answers

1


For the logic you are trying to implement you can go through all the arrays you have inside the main and for each of them check if it contains the value you are looking for using the function array_search. This function returns the key associated with the value or false if it does not exist. When it exists and is not the first element you find assigns the value '' to clean up.

Example of implementation:

function removerValoresDuplicadas(&$array, $valor){
    $primeiroEncontrado = false;

    for ($i = 0; $i < count($array); ++$i){
        //obtem a chave associada ao valor ou false caso não exista nenhuma
        $chave = array_search($valor, $array[$i]); 

        if ($chave !== false){ //se existe o valor
            if ($primeiroEncontrado === false){ //caso especial para não limpar o primeiro
                $primeiroEncontrado = true;
            }   
            else {
                $array[$i][$chave] = ''; //limpa
            }
        }
    }
}

$dados = Array(
        Array("forn_nome_fantasia" => "FORNECEDOR TESTE",
              "class_nvl4_descricao" => "DESPESAS COM FRETES" ),
        Array("forn_nome_fantasia" => "DJ/ DESEO -SALA DE ESTAR",
              "class_nvl4_descricao" => "DESPESAS COM FRETES" )
    );


removerValoresDuplicadas($dados, "DESPESAS COM FRETES");
print_r($dados);

Exit:

Array
(
    [0] => Array
        (
            [forn_nome_fantasia] => FORNECEDOR TESTE
            [class_nvl4_descricao] => DESPESAS COM FRETES
        )

    [1] => Array
        (
            [forn_nome_fantasia] => DJ/ DESEO -SALA DE ESTAR
            [class_nvl4_descricao] => 
        )

)

Note that I have built a function that receives the value you want to delete from the array to make it more flexible and be able to easily delete others you want

Example in Ideone

1

This code will leave all keys empty class_nvl4_descricao duplicated, whatever the value, leaving only the first occurrence:

Considering the name of the parent array = $array (if it is another name, just replace in the code).

<?php
foreach($array as $idx => $texto){
   for($x=$idx+1;$x<sizeof($array);$x++){
      if( $array[$idx]['class_nvl4_descricao'] == $array[$x]['class_nvl4_descricao'] ){
         $array[$x]['class_nvl4_descricao'] = '';
      }
   }
}
?>

Ideone.

Browser other questions tagged

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