Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Aim. Multiple times (in multiple places) need to check value of array key and based on the value inside foreach need to echo currency code.

Have array (as example), named $data_pvn_1_ii_each_invoice_debit

Array ( 
[0] => Array ( [VatCodeCountryCode] => IE [VatCode] =>123456 ) 
[1] => Array ( [VatCodeCountryCode] => GB [VatCode] =>958725 ) 
)

Here define variable which includes abbreviations of countries, where currency is euro.

$country_codes_with_euro_currency = array( 'AT', 'BE', 'CY', 'DE', 'EE', 'GR', 'ES', 'FI', 'FR', 'IE', 'IT', 'LU', 'MT', 'NL', 'PT', 'SI', 'SK' );

Then multiple times (in multiple places) need to check value of [VatCodeCountryCode] and based on country code need to echo currency

At first trying to get [VatCodeCountryCode].

foreach($data_pvn_1_ii_each_invoice_debit as $i => $result){
$trim_result_vat_country_code = trim($result[VatCodeCountryCode]);
}

Then function (part of function)

function myTest($trim_result_vat_country_code) {

if ( in_array($trim_result_vat_country_code), $country_codes_with_euro_currency) ) {
return $currency_code = 'EUR'; 
}

elseif ( $trim_result_vat_country_code == 'GB' ) {
return $currency_code = 'GBP';
}   

}

And then need to echo currency code (also only part of code)

<?php foreach($data_pvn_1_ii_each_invoice_debit as $i => $result){?>
<tr><td>
<?php echo myTest($trim_result_vat_country_code); ?>
</td></tr>
<tr><td>content of other td</td></tr>
<?php }?>

First problem: code does not work with ( in_array($trim_result_vat_country_code), $country_codes_with_euro_currency) )

Second problem: echo myTest($trim_result_vat_country_code); returns only last result. For array key VatCodeCountryCode there are values IE and GB. So need to echo currencies EUR and GBP. But echo only GBP

share|improve this question

1 Answer 1

up vote 3 down vote accepted

First Problem :

if( in_array($trim_result_vat_country_code), $country_codes_with_euro_currency))

should be

if( in_array($trim_result_vat_country_code, $country_codes_with_euro_currency))

In in_array function second parameter should be an array.

Second Problem :

foreach($data_pvn_1_ii_each_invoice_debit as $i => $result){
  $trim_result_vat_country_code = trim($result[VatCodeCountryCode]);
}

Your above code holds only the last record.That is why myTest function returns only last records.

Solution :

<?php foreach($data_pvn_1_ii_each_invoice_debit as $i => $result){?>
<tr><td>
<?php echo myTest($result['VatCodeCountryCode']); ?>
</td></tr>
<tr><td>content of other td</td></tr>
<?php }?>

Modifiy your myTest function

function myTest($trim_result_vat_country_code) {
    global $country_codes_with_euro_currency;
    if ( in_array($trim_result_vat_country_code, $country_codes_with_euro_currency) ) {
        return $currency_code = 'EUR';
    } elseif ( $trim_result_vat_country_code == 'GB' ) {
        return $currency_code = 'GBP';
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.