PHP:将数组输出存储在变量中以供以后使用
问题描述:
Script taken from http://css-tricks.com/snippets/php/get-geo-ip-information/
.
.
.
//Array where results will be stored
$ipInfo=array();
//check response from ipserver for above patterns
foreach ($patterns as $key => $pattern)
{
//store the result in array
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}
return $ipInfo;
}
This displays the $ipInfo.
The thing is that i don't want it to display $ipInfo. I want to save the output in a variable and then echo it later, when i need it in another place. How can i do that?
脚本取自 http://css-tricks.com/snippets/php/get-geo-ip-information/ p>
。
。
。
//将存储结果的数组
$ ipInfo = array();
//检查ipserver对上述模式的响应
foreach($ patterns as $ key => $ pattern)
{
//将结果存储在数组
$ ipInfo [$ key] = preg_match($ pattern,$ response,$ value)&& !空($ value [1])? $ value [1]:'not found';
}
返回$ ipInfo;
}
code> pre>
这显示 $ ipInfo strong>。 p>
问题是我不希望它显示 $ ipInfo strong>。 我希望将输出保存在变量中,然后稍后回显 strong>,当我需要它在另一个地方时。 我该怎么做? p>
div>
答
print_r(geoCheckIP($ip));
This is what's doing the outputting. Just assign to a variable instead:
$somevar = geoCheckIP($ip);
This is trivial PHP that you really should know.