YurkamTim is right. It needs only a modification:
After function($) you need a pointer to the external variable by "use(&$searchedValue)" and then you can access the external variable. Also you can modify it.
$neededObject = array_filter(
$arrayOfObjects,
function ($e) use (&$searchedValue) {
return $e->id == $searchedValue;
}
);