PHP array_filter带有回调参数

时间:2014-07-29 22:19:33

标签: php arrays filtering

我需要在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,但我无法让它发挥作用。任何想法??

4 个答案:

答案 0 :(得分:3)

// even using a global variable doesn't 
// make it visible in getUniv() function
global $univ_seg;
$univ_seg = 'FSU';

这不是全局变量的工作原理 - 要访问全局变量,您需要在要使用它的每个范围内使用匹配的global声明。

function getUniv($var){
    global $univ_seg;
    return($var["EventType"] == "UN" && $var["LocationURL"] == $univ_seg);
}

这可以作为匿名函数更好地工作:

$re = array_filter($univ_sched, function getUniv($var) use ($univ_seg) {
    return($var["EventType"] == "UN" && $var["LocationURL"] == $univ_seg);
});

答案 1 :(得分:0)

为什么不尝试使用匿名函数呢?我发现回调功能更好用

function getUA($data, $univ_seg) {
    $univ_sched = json_decode($data, true);
    $re = array_filter($univ_sched, function($var){
        return($var["EventType"] == "UN" && $var["LocationURL"] == 'FSU');
    });
    print_r($re); 
}

答案 2 :(得分:0)

我使用对象http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback

让它工作
class univFilter {
    public $univ_seg;
    public function filter($var) {
        if($var["EventType"] == "UN" && $var["LocationURL"] == $this->univ_seg)
            return true;
          else
            return false;
    }
}

$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/11/30","LocZipCode"=>"92108","LocationURL"=>"BU","EventType"=>"UN"),
    Array("StartDate"=>"2014/09/30","LocZipCode"=>"78661","LocationURL"=>"BU","EventType"=>"UN")
));

$univ_seg = "BU";
getUA($data,$univ_seg);

function getUA($data, $univ_seg) {
    $f = new univFilter();
    $f->univ_seg = $univ_seg;
    $univ_sched = json_decode($data, true);
    $data       = array_filter($univ_sched,array($f,"filter"));
    print_r($data);
}

答案 3 :(得分:0)

你可以使用lambda函数:

$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")
));

$univ_seg = 'FSU';

getUA($data, $univ_seg);

function getUnivFunc($univ_seg){
    return create_function('$a','return $a["EventType"] == "UN" && $a["LocationURL"] == "' . $univ_seg . '";');
}

function getUA($data, $univ_seg) {
    $univ_sched = json_decode($data, true);
    $re = array_filter($univ_sched, getUnivFunc($univ_seg));
    print_r($re); 
}

http://codepad.org/LnVVry5L

给出:

Array
(
    [0] => Array
        (
            [StartDate] => 2014/07/31
            [LocZipCode] => 19406
            [LocationURL] => FSU
            [EventType] => UN
        )

    [1] => Array
        (
            [StartDate] => 2014/08/31
            [LocZipCode] => 23513
            [LocationURL] => FSU
            [EventType] => UN
        )

)