Revamp
Revamp

Reputation: 30

php array filter with custom arguments

     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
        )

)

I have used this function to filter array

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;
}

should be able to send

'https://yyyyy.yyy' ,url, click, action to function

How to combine these two functions into one function such that I can send action and URL to this function and fetch the correct result

Upvotes: 0

Views: 2677

Answers (1)

Don't Panic
Don't Panic

Reputation: 41810

Use an anonymous function instead of a defined function if you want to pass values into the scope of the callback.

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

Upvotes: 3

Related Questions