Remove duplicate array element

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 class_nvl4_descricao key are the same. I want to remove this key when it is repeated. Preferably remove all and leave only one.

I wanted it to look like this:

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] =>   ''

  )
)
    
asked by anonymous 16.11.2017 / 18:05

2 answers

1

For the logic that you are trying to implement, you can go through all the arrays inside the principal, and for each one of them, check if it contains the value you are looking for using the array_search function. 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 it finds, it assigns the value '' to clear.

Implementation example:

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);

Output:

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] => 
        )

)

Notice that I built a function that gets the value that you want to delete from the array to make it more flexible and easily eliminate others you want

Example on Ideone

    
17.11.2017 / 00:16
1

This code will empty all duplicate class_nvl4_descricao keys, regardless of what value is, leaving only the first occurrence:

Considering the name of the parent array = $array (if it is another name, just replace it 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 .

    
17.11.2017 / 00:24