我在PHP
中有一个多维数组,代码如下:
<?php
$weather = array (
"AVAILABLE" => array (
"first" => "We're having a nice day.",
"second" => "We're not having a nice day.",
"fifth" => "It's raining out there.",
"tenth" => "It's gonna be warm today."));
function getDomain() {
if (strpos($_SERVER['SERVER_NAME'], '.com') !== FALSE) {
return "first";
}
elseif (strpos($_SERVER['SERVER_NAME'], '.eu') !== FALSE) {
return "second";
}
else {
die();
}
}
function myWeather($string) {
$domain = getDomain();
return $weather[$string][$domain];
}
echo myWeather("AVAILABLE");
?>
当我使用域.com访问网络时,它应该回显键值&#34; AVAILABLE&#34;在域名密钥中(&#34;首先&#34;) - 我们度过了美好的一天。
当我在域名.eu的网站上时,它应该写入密钥&#34; AVAILABLE&#34;的值,但在另一个域密钥(&#34; second&#34;) - 我们&# 39;没有美好的一天。
我该如何使这项工作好吗?稍后,数组$weather
中会有更多键。
答案 0 :(得分:2)
需要将数组作为 参数 添加到函数myWeather()
中: -
<?php
$weather = array (
"AVAILABLE" => array (
"first" => "We're having a nice day.",
"second" => "We're not having a nice day.",
"fifth" => "It's raining out there.",
"tenth" => "It's gonna be warm today."
)
);
function getDomain() {
if (strpos($_SERVER['SERVER_NAME'], '.com') !== FALSE) {
return "first";
}
elseif (strpos($_SERVER['SERVER_NAME'], '.eu') !== FALSE) {
return "second";
}
else {
return "fifth"; // instead of die(); return some value
}
}
function myWeather($string,$array) {
$domain = getDomain();
return $array[$string][$domain];
}
echo myWeather("AVAILABLE",$weather);
?>
注意: - 以上需要在 功能范围
中 数组工作输出: - https://eval.in/841524