从PHP中的数组返回匹配值

时间:2016-05-10 23:07:11

标签: php arrays multidimensional-array

我正在尝试返回基于$ ProductCurrency的货币符号和货币代码,其值是数组第一列中的货币之一,这是我的代码;

<?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返回任何内容,我期待{$ 1}}和$ CurrencyCode,我期待'aud',变量,我缺少什么?

2 个答案:

答案 0 :(得分: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];

在分配$ 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 ('&#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