2016-05-10 79 views
-2

我試圖返回基於$ ProductCurrency的貨幣符號和貨幣代碼,其值是數組中第一列中的貨幣之一,這是我擁有的代碼;從PHP中的數組返回匹配值

<?php 

    $currency_symbols = array (

     array ('australian dollar','&#36;','aud'), 
     array ('brazilian real','&#82;&#36;','brl'), 
     array ('canadian dollar','&#36;','cad'), 
     array ('czech koruna','&#75;&#269;','czk'), 
     array ('danish krone','&#107;&#114;','dkk'), 
     array ('euro','&#8364;','euro'), 
     array ('pound sterling','&#163;','gbp'), 
     array ('hong kong dollar','&#36;','hkd'), 
     array ('hungarian forint','&#70;&#116;','huf'), 
     array ('israeli new sheqel','&#8362;','ils'), 
     array ('japanese yen','&#165;','jpy'), 
     array ('malaysian ringgit','&#82;&#77;','myr'), 
     array ('mexican peso','&#36;','mxn'), 
     array ('norwegian krone','&#107;&#114;','nok'), 
     array ('new zealand dollar','&#36;','nzd'), 
     array ('philippine peso','&#8369;','php'), 
     array ('polish zloty','&#122;&#322;','pln'), 
     array ('russian ruble','&#1088;&#1091;&#1073;','rub'), 
     array ('singapore dollar','&#36;','sgd'), 
     array ('swedish krona','&#107;&#114;','sek'), 
     array ('swiss franc','&#67;&#72;&#70;','chf'), 
     array ('thai baht','&#3647;','thb'), 
     array ('turkish lira','&#8356;','try'), 
     array ('taiwan new dollar','&#78;&#84;&#36;','twd'), 
     array ('us dollar','&#36;','usd'), 
    ); 

    $ProductCurrency = 'australian dollar'; 
    $CurrencySymbol = $currency_symbols[$ProductCurrency][1]; 
    $CurencyCode = $currency_symbols[$ProductCurrency][2]; 

?> 

但代碼爲$ CURRENCYSYMBOL返回沒有爲我所期待&#36;和$貨幣代碼爲此我要求以「澳元」,變量,我缺少什麼?

+1

什麼'get_field'?你使用WP? –

+0

*「我錯過了什麼?」* - 更多(相關)代碼。同時,看看http://php.net/manual/en/function.error-reporting.php是否返回任何內容。 –

+0

是的,它是WordPress,get_field('product_currency')返回一個用戶選擇,它基於與數組中第1列相同的列表。 '澳大利亞元',或'巴西真實',這工作正常,所以你可以假設$ ProductCurrency =澳大利亞元。 – Naz

回答

1

您使用$ProductCurrency作爲重點查找符號和代碼,這個工作你有你的陣列格式如下:

<?php 

$currency_symbols = array (

    'australian dollar' => array ('&#36;','aud'), 
    'brazilian real' => array ('&#82;&#36;','brl'), 
    // ... 
); 

$ProductCurrency = get_field('product_currency'); 
$CurrencySymbol = $currency_symbols[$ProductCurrency][0]; 
$CurencyCode = $currency_symbols[$ProductCurrency][1]; 

您應該在數組中可能check if the key exists分配$前CurrencySymbol和$ CurrencyCode。

如果你想/必須保持$ 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

我沒有看到太多的邏輯陣列上,特別是如果你需要搜索某個特定key,這裏有一個不同的方法:

<?php 
$currency_symbols = array(
    "australian dollar" => array ('&#36;','aud'), 
    "brazilian real" => array ('&#82;&#36;','brl'), 
    "canadian dollar" => array ('&#36;','cad'), 
    "czech koruna" => array ('&#75;&#269;','czk'), 
    "danish krone" => array ('&#107;&#114;','dkk'), 
    "euro" => array ('&#8364;','euro'), 
    "pound sterling" => array ('&#163;','gbp'), 
    "hong kong dollar" => array ('&#36;','hkd'), 
    "hungarian forint" => array ('&#70;&#116;','huf'), 
    "israeli new sheqel" => array ('&#8362;','ils'), 
    "japanese yen" => array ('&#165;','jpy'), 
    "malaysian ringgit" => array ('&#82;&#77;','myr'), 
    "mexican peso" => array ('&#36;','mxn'), 
    "norwegian krone" => array ('&#107;&#114;','nok'), 
    "new zealand dollar" => array ('&#36;','nzd'), 
    "philippine peso" => array ('&#8369;','php'), 
    "polish zloty" => array ('&#122;&#322;','pln'), 
    "russian ruble" => array ('&#1088;&#1091;&#1073;','rub'), 
    "singapore dollar" => array ('&#36;','sgd'), 
    "swedish krona" => array ('&#107;&#114;','sek'), 
    "swiss franc" => array ('&#67;&#72;&#70;','chf'), 
    "thai baht" => array ('&#3647;','thb'), 
    "turkish lira" => array ('&#8356;','try'), 
    "taiwan new dollar" => array ('&#78;&#84;&#36;','twd'), 
    "us dollar" => array ('&#36;','usd') 
); 

$ProductCurrency = get_field('product_currency'); //us dollar 
$CurrencySymbol = $currency_symbols[$ProductCurrency][0]; 
$CurencyCode = $currency_symbols[$ProductCurrency][1]; 
echo $CurrencySymbol; //&#36; 
echo $CurencyCode; //usd 

Ideone Demo

+0

這是很多$ currency_symbols – Naz

+0

如果你不喜歡'$ currency_symbols',檢查更新的答案,它沒有太多代碼一樣完全一樣。 –