0

標的。複数回 (複数の場所で) 配列キーの値を確認する必要があり、内部の値に基づいてforeach通貨コードをエコーする必要があります。

名前付きの配列を (例として) 持つ$data_pvn_1_ii_each_invoice_debit

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

ここでは、通貨がユーロである国の略語を含む変数を定義します。

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

次に、複数回(複数の場所で)[VatCodeCountryCode]国コードの値を確認し、それに基づいて通貨をエコーする必要があります

最初は取得しようとしています[VatCodeCountryCode]

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

次に機能(機能の一部)

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';
}   

}

そして、通貨コードをエコーする必要があります(コードの一部のみ)

<?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 }?>

最初の問題: コードが動作しない( in_array($trim_result_vat_country_code), $country_codes_with_euro_currency) )

2 番目の問題:echo myTest($trim_result_vat_country_code);最後の結果のみを返します。配列キーVatCodeCountryCodeには、値IEとがありますGBEURしたがって、通貨とをエコーする必要がありGBPます。ただし、エコーのみGBP

4

1 に答える 1

3

最初の問題:

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

する必要があります

if( in_array($trim_result_vat_country_code, $country_codes_with_euro_currency))

in_array関数の 2 番目のパラメーターはarray.

2番目の問題:

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

上記のコードは最後のレコードのみを保持します。そのため、myTest関数は最後のレコードのみを返します。

解決 :

<?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 }?>

myTest関数を変更します

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';
    }
}
于 2013-08-01T11:25:45.980 に答える