目前我正在使用 php geoip_country_code_by_name 函数从一个数组中为不同的国家提供不同的内容,如下所示:
<?php
$content = array(
'GB' => array(
'meta_description' => "Description is here",
'social_title' => "Title here",
'country_content_js' => "js/index.js",
),
'BR' => array(
'meta_description' => "Different Description is here",
'social_title' => "Another Title here",
'country_content_js' => "js/index-2.js",
),
);
?>
但我只有针对巴西和英国的特定内容。我希望访问该页面的任何其他国家/地区都可以使用与 BR 和 GB 不同的默认内容数组。
有没有办法创建一个规则,向我的数组中未指定的任何国家/地区提供一组默认内容?
最佳答案
$content = array(
'GB' => array(
'meta_description' => "Description is here",
'social_title' => "Title here",
'country_content_js' => "js/index.js",
),
'BR' => array(
'meta_description' => "Different Description is here",
'social_title' => "Another Title here",
'country_content_js' => "js/index-2.js",
)
);
您可以像这样使用另一个“默认”键来引用一个键;
$content['Default'] =& $content["GB"];
var_dump($content);
exit;
或者,如果您订购了从 DB 或其他任何地方返回的值,您可以像这样读取数组的第一个条目; $aDefault =& $content[array_keys($content)[0]];
或者您可以定义默认语言并读取该数组键,但与之前的方法不同,它必须在数组中。
// define default
define("DEFAULT_LANGUAGE", 'GB');
// would need to guarentee its there
$aDefault =& $content[DEFAULT_LANGUAGE];
最后你可以结合上面的,所以如果它找不到你可以使用第一个可用的语言;
// define, can be placed in an included config folder
define("DEFAULT_LANGUAGE", 'GB');
$content = array(
'GBs' => array(
'meta_description' => "Description is here",
'social_title' => "Title here",
'country_content_js' => "js/index.js",
),
'BR' => array(
'meta_description' => "Different Description is here",
'social_title' => "Another Title here",
'country_content_js' => "js/index-2.js",
)
);
// does the default language exist?
if( isset($content[DEFAULT_LANGUAGE]) ){
// yes, create a default array key and reference the required element in the array
$content['Default'] =& $content[DEFAULT_LANGUAGE];
}else{
// no, create a default array key and reference the first element
$content['Default'] =& $content[array_keys($content)[0]];
}
var_dump($content);
exit;
关于php - 我可以使用 geoip_country_code_by_name 向多个国家/地区提供特定内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39815099/