目的。多次(在多个地方)需要检查数组键的值,并根据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) )
第二个问题:echo myTest($trim_result_vat_country_code);
仅返回最后一个结果。对于数组键VatCodeCountryCode
,值为IE
和GB
。因此需要回显货币EUR
和GBP
。但只回显GBP
答案 0 :(得分: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
函数中,第二个参数应为array
。
第二个问题:
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';
}
}