【发布时间】:2018-01-12 07:45:31
【问题描述】:
我在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 访问网络时,它应该在域键(“first”)中回显键“AVAILABLE”的值 - 我们度过了愉快的一天。
当我使用域 .eu 进入站点时,它应该写入键“AVAILABLE”的值,但在另一个域键(“第二”)中 - 我们今天过得不好。
请问我怎样才能完成这项工作?以后数组$weather会有更多的key。
【问题讨论】:
-
myWeather看不到$weather数组,因为它是在函数范围之外定义的
标签: php arrays variables multidimensional-array