1

I have some problems with GET request. This request has two parameters to filter:

$app->get('/api/v1/provider/{destination}/{weight}', function ($request, $response, $args) {

   $em = getEntityManager();
   $destination = $em->getRepository(Transport::class)->findByDestination($args['destination']);
   $weight = $em->getRepository(Transport::class)->findByWeight($args['weight']);

     $provider_filter = array_filter($destination, function ($data) {
        return $data->weight == $weight;    
    });

    return $response->withJson($provider_filter);
});

For example , if I write $dato->weight == 3 instead of $dato->weight == $weight, I get correct result. The problem is in the variable $weight, due to which $weight I get the error: Undefined variable: weight. The variable $weight is defined outside the array_filter. Nevertheless, if I defined $weight inside the array I can't get the weight because of the error: Undefined variable: args.

Any idea?

1 Answer 1

2

To use your variables inside closure/anonymous functions you need to pass them as use($weight) like

$provider_filter = array_filter($destination, function ($data) use($weight) {
    return $data->weight == $weight;    
}); 
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.