7

how can I search an array matching two or more values?

Array
(
    [1440972000] => Array
        (
            [mitarbeiterid] => 1
            [von] => 1441006800
            [doppeltermin] => n
            [stundentermin] => n
            [abgesagt] => n
        )
)

I would like to search for "mitarbeiterid" and "von". This is just a example, in this array are a few hundred entries.

I only know how to search for e.g. "von" but how to combine the search parameters?

1
  • Maybe you can use some recursive function. Commented Sep 1, 2015 at 13:53

1 Answer 1

13

Have a look at array_filter()

$filtered_array = array_filter($your_array, function($val){
              return ($val['mitarbeiterid']=='something' and $val['von']=='something');
          });

To use outside variables, inside lambda function, use use keyword

$var1 = 'something';
$var2 = 'something';
//                                                         ▼
$filtered_array = array_filter($your_array, function($val) use($var1, $var2){
    return ($val['mitarbeiterid']==$var1 and $val['von']==$var2);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, looks very good, but how do I pass some variables through to the callback function? Something like this $filtered_array = array_filter($your_array, function($val, $employees, $timestamp)...?
@viral if you are going to edit the answer, you might as well show the most modern syntax with arrow functions and the removal of use().

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.