Searching for selected values in Twig and PHP

3086 views php
6

I have this array in php:

Array
(
    [pageHeader] => Array
        (

            [pageValue] => Array
                (


                    [attractionsArray] => Array
                        (
                            [0] => Array
                                (
                                    [tourist_attractions_id] => 4
                                    [enable] => 1
                                    [name_pl] => atrackja 2
                                )

                            [1] => Array
                                (
                                    [tourist_attractions_id] => 1
                                    [enable] => 1
                                    [name_pl] => nazwa pl1
                                )

                        )

                    [selectedAttractionsArray] => Array
                        (
                            [0] => Array
                                (
                                    [related_id] => 4
                                )

                        )

                )

        )

and my twig file:

{% for attractions in pageTemplate.pageHeader.pageValue.attractionsArray %}
   <fieldset class="checkboxsas">
                                            <label>
                                                <input type="checkbox" value="{{ attractions.tourist_attractions_id }}" {% if attractions.tourist_attractions_id in selectedAttractionsArray %} checked {% endif %}> {{ attractions.name_pl }}
                                            </label>
                                        </fieldset>
                                    {% endfor %}

I would like to check the checkbox as checked for records that: attractionsArray.tourist_attractions_id == selectedAttractionsArray.related_id

How to do it?

answered question

1 Answer

11

Try:

{% if attractions.tourist_attractions_id == pageTemplate.pageHeader.pageValue.selectedAttractionsArray.0.related_id %} checked {% endif %}

posted this

Have an answer?

JD

Please login first before posting an answer.