Php array check for values or keys

2
[TotalPed] => Array
    (
        [VlrTotalIPI] => 0.00
        [VlrTotalItens] => 652.68
        [VlrTotalDesc] => 0.00
        [VlrTotalPed] => 652.68
        [PercComPed] => 0.00
        [PercComPed2] => 0.00
        [VlrFrete] => 0.00
        [TotalBaseIcms] => 0.00
        [VlrTotalIcms] => 0.00
        [VlrTotalBaseIcmST] => 0.00
        [VlrTotalIcmST] => 0.00
        [vFCPUFDestTot] => 0.00
        [vICMSUFDestTot] => 0.00
        [vICMSUFRemetTot] => 0.00
    )

I have this array in a $ myArray variable. I would like to know how to check if the fields exist (keys).

Example: [VlrTotalIPI] if there is no need to say that you need this field ( I believe you have to compare with an array where only the required fields are ).

And also if there are values for these keys, again if it does not exist, point to the key that needs value. Using clear function, to do this independent of the Array.

    
asked by anonymous 10.11.2016 / 01:54

1 answer

2

If you need to check separately you can use array_key_exists () , or isset () for the key name, and empty () for the value.

if( array_key_exists( 'VirTotalPI', $meuArray ) ) {
    // chave existe
   if( ! empty( $meuArray['VirTotalPI'] ) {
       // não está vazio
   }
}

If it can be all together, empty() solves everything by itself (returns true if it does not exist or if it is empty)

    
10.11.2016 / 01:57