[英]PHP Function Inside of An Array?
Here is what I have so far: 这是我到目前为止:
$arrayPrices = array(
translate($lang_type, "A/C System Evaluation") => "19.95",
translate($lang_type, "A/C Evaluation & Recharge") => "99.00"
);
And my translate function is: 我的翻译功能是:
function translate($to_lan, $text) {
if($to_lan == "en") {
return $text;
} else {
$translate_feed = @file_get_contents('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=' . BING_APPID . '&text=' . urlencode($text) . '&from=en&to=' . $to_lan . '');
$translate = simplexml_load_string($translate_feed);
return ($translate_feed === false) ? $text : $translate[0];
}
}
For some reason, I can't display that translate function inside of my PHP Array. 出于某种原因,我无法在PHP数组中显示该翻译功能。
If I type in echo translate($lang_type, "A/C System Evaluation");
如果我输入
echo translate($lang_type, "A/C System Evaluation");
it works just fine and translates. 它工作正常并翻译。 But when used in that array it just returns blank.
但是当在该数组中使用时,它只返回空白。
Does anyone have any idea what I can do? 有谁知道我能做什么?
From the PHP Array docs : 从PHP数组文档 :
The key can either be an integer or a string.
密钥可以是整数或字符串。 The value can be of any type.
值可以是任何类型。
Put your keys in string vars first,like: 首先将您的密钥放在字符串变量中,例如:
$var1 = translate($lang_type, "A/C System Evaluation");
$var2 = translate($lang_type, "A/C Evaluation & Recharge");
$arrayPrices = array(
"$var1" => 19.95
"$var2" => 29.95
);
That should work fine. 这应该工作正常。
does this work: 这工作:
$arrayPrices[translate($lang_type, "A/C System Evaluation")]= "19.95";
$arrayPrices[translate($lang_type, "A/C Evaluation & Recharge")] = "99.00";
I presume you want to be able to add extensively to that product list without having to mess around with temporary variables a great deal. 我认为你希望能够广泛地添加到该产品列表中,而不必大量使用临时变量。 This is one of those situations where I'd do a post-processing run on the array, like so:
这是我在数组上进行后处理运行的情况之一,如下所示:
$arrayPrices = array(
"A/C System Evaluation" => "19.95",
"A/C Evaluation & Recharge" => "99.00",
// ... etcetera ...
);
$keys = array_keys( $arrayPrices );
foreach( $keys as $keyText )
{
$translatedKeyText = translate($lang_type, $keyText);
if ( $translatedKey != $keyText )
{
$arrayPrices[$translatedKeyText] = $arrayPrices[$keyText];
unset( $arrayPrices[$keyText] );
}
}
If you use temporary variables, you'll have to add logic for every new entry to your original array. 如果使用临时变量,则必须为原始数组的每个新条目添加逻辑。 That sounds like a maintenance hassle to me.
这对我来说听起来像是一个维护麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.