Php array check for values or keys

Asked

Viewed 1,930 times

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 variable $meuArray. Like to know how to check if the fields exist (Keys).

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

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

1 answer

2


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

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

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

Browser other questions tagged

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