Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if specific key array element exists?

i have my view in twig

and i have an array which has or hasnt key value i need how to check it if its exist ?

example {{ weather.wind.deg }} and for current moment its possible there is no wind deg so the weather.wind array will not contain an element witch key deg how to check it if it has it or no ? maybe i should do it before i past this to my view ? somewhere here ?

$app->get('/', function () use ($app) {
    return $app['twig']->render('index.html.twig', array(
            'weather' => $app['weather_service']->get($app['location_service']->get()),
            'location' => $app['location_service']->get())
    );
});
like image 648
vardius Avatar asked Mar 22 '23 10:03

vardius


1 Answers

In you twig template you can do :

{% if weather.wind.deg is defined %}
        make your things
{% endif %}
like image 167
BENARD Patrick Avatar answered Apr 06 '23 07:04

BENARD Patrick