0

我在 PHP 中有一个数组:

$wxCode = array(
    'VC' => 'nearby',
    'MI' => 'shallow',
    'PR' => 'partial',
    'BC' => 'patches of',
    'DR' => 'low drifting',
    'BL' => 'blowing',
    'SH' => 'showers',
    'TS' => 'thunderstorm',
    'FZ' => 'freezing',
    'DZ' => 'drizzle',
    'RA' => 'rain',
    'SN' => 'snow',
    'SG' => 'snow grains',
    'IC' => 'ice crystals',
    'PE' => 'ice pellets',
    'GR' => 'hail',
    'GS' => 'small hail',  // and/or snow pellets
    'UP' => 'unknown',
    'BR' => 'mist',
    'FG' => 'fog',
    'FU' => 'smoke',
    'VA' => 'volcanic ash',
    'DU' => 'widespread dust',
    'SA' => 'sand',
    'HZ' => 'haze',
    'PY' => 'spray',
    'PO' => 'well-developed dust/sand whirls',
    'SQ' => 'squalls',
    'FC' => 'funnel cloud, tornado, or waterspout',
    'SS' => 'sandstorm/duststorm');

如果在这里,我有一点:

    if (substr($part,0,1) == '-') {
        $prefix = 'light ';
        $part = substr($part,1);
    }
    elseif (substr($part,0,1) == '+') {
        $prefix = 'heavy ';
        $part = substr($part,1);
    }
    else $prefix = '';

但是,我想编写一个函数,根据数组和 if: light rain 将以下字符串:-RA 转换为以下文本。

我该怎么做?

4

2 回答 2

2

你几乎就在那里。只需用作数组$part的键:$wxCode

$wxCode[$part]

如果您没有前缀或:

$wxCode[substr($part, 1)]

如果你这样做。您可能需要使用strtoupper()以确保两个字母的代码全部大写

于 2012-08-24T22:45:21.400 回答
1
$str = str_replace(array('-', '+'), array('light ', 'heavy '), $str);
$str = str_replace(array_keys($wxCode), array_values($wxCode), $str);
于 2012-08-24T22:55:32.100 回答