Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: multi-column search in array

Tags:

arrays

php

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?

like image 574
rwur Avatar asked Sep 01 '15 13:09

rwur


1 Answers

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);
});
like image 106
viral Avatar answered Oct 18 '22 01:10

viral