0
     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

5
  • 1
    what's the problem in using the two comparisons in one function? that'll be combining them right? Commented Sep 15, 2017 at 15:05
  • @YashKumarVerma yes i need to combine these function into one function Commented Sep 15, 2017 at 15:06
  • Check: stackoverflow.com/questions/5482989/… Commented Sep 15, 2017 at 15:06
  • 1
    just merge the two conditions ? does that work ? Commented Sep 15, 2017 at 15:07
  • @YashKumarVerma` if (isset($element->action) && $element->action == 'click' isset($element->url) && $element->url == 'yyyyy.yyy')) { return TRUE; } ` Commented Sep 15, 2017 at 15:09

1 Answer 1

3

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;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.