Suppose,
$data = array(
array('id' => 1, 'user_id' => 1, 'assignment_id' => 1, 'grade' => 90),
array('id' => 2, 'user_id' => 3, 'assignment_id' => 2, 'grade' => 85),
array('id' => 3, 'user_id' => 5, 'assignment_id' => 5, 'grade' => 66),
);
Now I want to filter the rows like following:
$rules = array(
'user_id' => 5,
'assignment_id' => 5
);
This should return the row at $data[2]
.
$rules = array(
'user_id' => 3,
'assignment_id' => 2,
'grade' => 85
);
will return $data[1]
.
Here order of keys
may be different both in $data
elements and $rules
.
I tried with array_intersect
, but that is not working for me.
If you just need to return a list of of the elements in
$data
which match the filtering criteria, you can use a combination ofarray_filter()
andarray_intersect_assoc()
to do the job:Note that you need PHP >= 5.3.0 to utilize the anonymous function as shown.