【发布时间】:2014-09-21 10:51:01
【问题描述】:
我需要在 PHP 中过滤一个数组,但不知道如何将参数传递给回调。本质上,我要对数组中的每个项目进行 2 次比较。
// This data will be sent to the function as JSON so I'm "creating" the JSON here.
$data = json_encode(Array(
Array("StartDate"=>"2014/07/31","LocZipCode"=>"19406","LocationURL"=>"FSU","EventType"=>"UN"),
Array("StartDate"=>"2014/08/31","LocZipCode"=>"23513","LocationURL"=>"FSU","EventType"=>"UN"),
Array("StartDate"=>"2014/07/31","LocZipCode"=>"92108","LocationURL"=>"BU","EventType"=>"UN"),
Array("StartDate"=>"2014/09/30","LocZipCode"=>"78661","LocationURL"=>"BU","EventType"=>"UN")
));
// even using a global variable doesn't
// make it visible in getUniv() function
global $univ_seg;
$univ_seg = 'FSU';
getUA($data, $univ_seg);
function getUniv($var){
return($var["EventType"] == "UN" && $var["LocationURL"] == $univ_seg);
}
function getUA($data, $univ_seg) {
$univ_sched = json_decode($data, true);
$re = array_filter($univ_sched, "getUniv");
print_r($re);
}
我也尝试过使用 lambda,但我无法让它工作。有什么想法吗??
【问题讨论】:
-
如果你想让这个变量对你的
getUniv()函数是全局的,把全局语句放在函数里面。