我正在尝试返回基于$ ProductCurrency的货币符号和货币代码,其值是数组第一列中的货币之一,这是我的代码;
<?php
$currency_symbols = array (
array ('australian dollar','$','aud'),
array ('brazilian real','R$','brl'),
array ('canadian dollar','$','cad'),
array ('czech koruna','Kč','czk'),
array ('danish krone','kr','dkk'),
array ('euro','€','euro'),
array ('pound sterling','£','gbp'),
array ('hong kong dollar','$','hkd'),
array ('hungarian forint','Ft','huf'),
array ('israeli new sheqel','₪','ils'),
array ('japanese yen','¥','jpy'),
array ('malaysian ringgit','RM','myr'),
array ('mexican peso','$','mxn'),
array ('norwegian krone','kr','nok'),
array ('new zealand dollar','$','nzd'),
array ('philippine peso','₱','php'),
array ('polish zloty','zł','pln'),
array ('russian ruble','руб','rub'),
array ('singapore dollar','$','sgd'),
array ('swedish krona','kr','sek'),
array ('swiss franc','CHF','chf'),
array ('thai baht','฿','thb'),
array ('turkish lira','₤','try'),
array ('taiwan new dollar','NT$','twd'),
array ('us dollar','$','usd'),
);
$ProductCurrency = 'australian dollar';
$CurrencySymbol = $currency_symbols[$ProductCurrency][1];
$CurencyCode = $currency_symbols[$ProductCurrency][2];
?>
但代码没有为$ CurrencySymbol返回任何内容,我期待{$ 1}}和$ CurrencyCode,我期待'aud',变量,我缺少什么?
答案 0 :(得分:1)
您正在使用$ProductCurrency
作为键来查找符号和代码,为此,您需要格式化数组,如下所示:
<?php
$currency_symbols = array (
'australian dollar' => array ('$','aud'),
'brazilian real' => array ('R$','brl'),
// ...
);
$ProductCurrency = get_field('product_currency');
$CurrencySymbol = $currency_symbols[$ProductCurrency][0];
$CurencyCode = $currency_symbols[$ProductCurrency][1];
在分配$ CurrencySymbol和$ CurrencyCode之前,您的阵列中应该check if the key exists。
如果您希望/必须按原样保留$ currency_symbols数组,则可以通过这种方式检索符号和代码:
$ProductCurrency = get_field('product_currency');
$filteredSymbols = array_filter($currency_symbols, function ($row) use ($ProductCurrency) {
return $row[0] === $ProductCurrency;
});
$CurrencySymbol = $filteredSymbols[0][1];
$CurrencyCode = $filteredSymbols[0][2];
效率较低,说实话,相当脏。你应该至少检查$ filteredSymbols是否为空。
答案 1 :(得分:1)
我的阵列上没有太多逻辑,特别是如果你需要搜索特定的key
,这里有一个不同的方法:
<?php
$currency_symbols = array(
"australian dollar" => array ('$','aud'),
"brazilian real" => array ('R$','brl'),
"canadian dollar" => array ('$','cad'),
"czech koruna" => array ('Kč','czk'),
"danish krone" => array ('kr','dkk'),
"euro" => array ('€','euro'),
"pound sterling" => array ('£','gbp'),
"hong kong dollar" => array ('$','hkd'),
"hungarian forint" => array ('Ft','huf'),
"israeli new sheqel" => array ('₪','ils'),
"japanese yen" => array ('¥','jpy'),
"malaysian ringgit" => array ('RM','myr'),
"mexican peso" => array ('$','mxn'),
"norwegian krone" => array ('kr','nok'),
"new zealand dollar" => array ('$','nzd'),
"philippine peso" => array ('₱','php'),
"polish zloty" => array ('zł','pln'),
"russian ruble" => array ('руб','rub'),
"singapore dollar" => array ('$','sgd'),
"swedish krona" => array ('kr','sek'),
"swiss franc" => array ('CHF','chf'),
"thai baht" => array ('฿','thb'),
"turkish lira" => array ('₤','try'),
"taiwan new dollar" => array ('NT$','twd'),
"us dollar" => array ('$','usd')
);
$ProductCurrency = get_field('product_currency'); //us dollar
$CurrencySymbol = $currency_symbols[$ProductCurrency][0];
$CurencyCode = $currency_symbols[$ProductCurrency][1];
echo $CurrencySymbol; //$
echo $CurencyCode; //usd