Check empty PHP array

Asked

Viewed 45 times

-1

I made a push array, however, has an attribute that I should put only when it has some value or greater than zero. Normally the table takes the value it has, but it cannot be zeroed. If it is zeroed the bank table puts the default value 0,000.

This is the array:

array_push($itens, 
    array(  
        'id' => $id,
        'preco_tabela'=> $preco,                                            
        'quantidade'=> $quantidade                      
        )                                   
);

I need him to stay that way when the $valorDesconto is greater than 0.000

array_push($itens, 
   array(  
       'id' => $id,
       'preco_tabela'=> $preco,                                            
       'quantidade'=> $quantidade, 
       'descontos_do_vendedor' => $valorDesconto                   
       )                                   
);

I tried to use the !Empty, but it didn’t work. Someone can help me?

Remembering that the value $valueDesconce is an array as well.

  • What’s wrong with leaving the field 'descontos_do_vendedor' => $valorDesconto blank when there is no value?

  • Although your question is about empty arrays, your problem has nothing to do with it.

  • it gives an error when inserting because the attribute cannot be empty since informed that has attribute, in the case of this as optional

  • I updated with a possible solution below.

2 answers

0


An idea for your problem check the value of $valorDesconto if it is greater than 0, assuming that array_push be inside while, for or something like.

$valorDesconto = ($valorDesconto > '0.00') ? $valorDesconto : '0.00'; 

array_push($itens, 
    array(  
        'id' => $id,
        'preco_tabela'=> $preco,                                            
        'quantidade'=> $quantidade,
        'descontos_do_vendedor' => $valorDesconto                  
    )                                   
);

CHECKING EMPTY ARRAYS

When I need to do this check I use two native php functions array_keys and array_values:

$arrCampo = array_keys($dados);
$arrValores = array_values($dados);
$numCampos = count($arrCampo);
$numValores = count($arrValores);

if($numCampos >= '1'){#seucodigoaqui}
if($numValores >= '1'){#seucodigoaqui}

This works by assuming that you will always send an array, with or without fields. Or function that can help is:

if(is_array($meuarray)){...}

That returns true or false if you are passing one array or no, she just checks the guy.

  • thanks for the help, I will be testing the solution and soon give a feedback

  • It worked, thank you very much

  • Good, do not forget to mark the answer as correct.

0

If I understand correctly your problem is not to check if the array is empty, but check if a value is empty before inserting it into array.

So what you should do is create the array before adding it to the list of the other array.

    $novoItem = [
        'id' => $id,
        'preco_tabela'=> $preco,                                            
        'quantidade'=> $quantidade
    ];
    //Usar [] é o mesmo que array()
    
    //Agora inserir no novo item o desconto apenas se ele for maior que 0
    $valorDesconto > 0 ? $novoItem['descontos_do_vendedor'] = $valorDesconto : null;
    
    /*
    O verificador ? funciona como um if else
    (Condição) ? Se Sim : Se não;
    E null = nada
    Isso é o mesmo que fazer com if:
    if($valorDesconto > 0){
      $novoItem['descontos_do_vendedor'] = $valorDesconto
    }
    */
    
    //Agora para inserir o novo item ao final do seu array de itens:
    $itens[] = $novoItem;
    
    //[] Insere ao final do array, assim como array_push

Browser other questions tagged

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