我有一些代码返回用户代理,通过一个函数运行它来解析它。 我之前使用的代码只返回解析函数中的一个变量(有三个:'platform''浏览器''版本'):
function my_get_user_agent($value) { $browser = parse_user_agent(); return isset($browser['platform']) ? $browser['platform'] : ''; }虽然此代码可以返回用户代理的平台,但我需要将其附加到函数中返回所有三个变量。 我将代码的前半部分更改为我认为正确的代码:
return isset($browser['platform'], $browser['browser'], $browser['version'])? $browser['platform'] : '';但是,我不确定我需要做些什么才能正确返回所有三个值。 建议?
I have a bit of code that returns the user agent, running it through a function to parse it. The code I have previously used returns only one variable from the parsing function (there are three: 'platform' 'browser' 'version'):
function my_get_user_agent($value) { $browser = parse_user_agent(); return isset($browser['platform']) ? $browser['platform'] : ''; }While this code works to return the platform of the user agent, I need to append it to return all three variables in the function. I changed the first half of the code to what I assume is correct:
return isset($browser['platform'], $browser['browser'], $browser['version'])? $browser['platform'] : '';I am unsure, however, as to what I need to do to properly return all three values. Suggestions?
最满意答案
你可以只返回整个数组:
return $browser;然后稍后访问这些值:
$browser['platform']; $browser['browser']; $browser['version'];再次阅读您的问题,您似乎想确保设置值。 你可以这样做:
foreach($browser as $value) { if(isset($value)) { $data[] = $value; } } return $data;现在数据将包含平台,浏览器和版本。
You can just return the entire array:
return $browser;Then access the values later:
$browser['platform']; $browser['browser']; $browser['version'];Reading your question again, you seem to want to ensure the value are set. You can do this:
foreach($browser as $value) { if(isset($value)) { $data[] = $value; } } return $data;Now data will contain platform, browser, and version.
更多推荐
发布评论