php数组过滤器与自定义参数

时间:2017-09-15 15:03:14

标签: php arrays

     Array
(
    [13] => stdClass Object
        (
            [action] => click
            [timestamp] => 2017-05-09T18:00:41+00:00
            [url] => https://xxxxx.xxx/xxx/sdfsdfsd
            [title] => download e-book
        )
     [14] => stdClass Object
        (
            [action] => click
            [timestamp] => 2017-05-09T18:00:41+00:00
            [url] => https://xxxxx.xxx/xxx/sdfsdfsd
            [title] => download e-book
        )

    [17] => stdClass Object
        (
            [action] => open
            [timestamp] => 2017-05-09T18:00:21+00:00
            [url] => https://yyyyyyy.yyy
            [title] => download e-book
        )

)

我用过这个函数来过滤数组

function filter_callback($element) {
    if (isset($element->action) && $element->action == 'click') {
        return TRUE;
    }
    return FALSE;
}

function filter_callback1($element) {
    if (isset($element->url) && $element->url == 'https://yyyyy.yyy') {
        return TRUE;
    }
    return FALSE;
}

应该能够发送

  

'https://yyyyy.yyy',网址,点击,操作   运作

如何将这两个函数组合成一个函数,以便我可以向此函数发送操作和URL并获取正确的结果

1 个答案:

答案 0 :(得分:2)

如果要将值传递到回调范围,请使用匿名函数而不是已定义的函数。

array_filter($your_array, function($element) use ($action, $url) {
    return isset($element->action, $element->url)
        && $element->action == $action
        && $element->url == $url;
});