在Laravel中回显多维PHP数组

a0zr77ik  于 2022-12-02  发布在  PHP
关注(0)|答案(2)|浏览(221)

我想在一个foreach循环中返回我的嵌套数组。在这个foreach循环中,我需要添加一个foreach,因为它是可变的,有多少可折叠的存在。
但是我不能让它把数据从“accordion”数组加载到前端,总是在某个地方出错。
我现在无法更好地描述它。
保存
这是我的treatments.php文件:

'treatments' => [
        [ /*  */
            'img' => asset('img/logos/Dermatologie_Dr_med_Aresu_Naderi_Nienstedten_Logo.png'),
            'title' => 'Klassische Dermatologie',
            'teaser' => 'Ein wesentlicher Bestandteil im Behandlungskonzept meiner Hautarztpraxis ist die ästhetische Dermatologie und Lasermedizin.',
            'pop-up' => 'derma',
            'accordion' => [
                [
              'title' => 'test',
              'content' => 'test2',
                ],
                [
                    'title' => 'test',
                    'content' => 'test2',
                ],
            ],
        ],
    ]

这是我的前端刀片代码:

<div class="row">
        @foreach( $treatments as $key => $value)
            <?php print_r($value) ?>
            <div class="col-md-4 py-5">
                <img class="w-100" src="{!! __($value['img']) !!}" alt="{!! __($value['title'])  !!}">
            </div>
            <div class="col-md-8 py-5">
                <span class="treatmentsHeadline">{!! __($value['title'])  !!}</span>
                <p class="pt-3">{!! __($value['teaser'])  !!}</p>
                <div class="pt-3">
                    <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Mehr erfahren</button>
                    <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Termin vereinbaren</button>
                </div>
            </div>
                @foreach($value as $acc)
                    <p>{!! __($acc['title']) !!}</p>
                @endforeach
        @endforeach
        <p class="footer-vh"></p>

    </div>

这是打印出来的数组:

kyxcudwk

kyxcudwk1#

@foreach($value as $acc)应该是@foreach($value['accordion'] as $acc)

9jyewag0

9jyewag02#

我终于找到了!

@php
    $i = -1;
@endphp
<div class="container-fluid px-0">
    <img class="w-100" src="{!! __('treatments.derma') !!}" alt="">
</div>
<div class="container my-4">
    <div class="row">
        @foreach( $treatments as $key => $value)
            @php
                $i++;
            @endphp

            <div class="col-md-4 py-5">
                <img class="w-100" src="{!! __($value['img']) !!}" alt="{!! __($value['title'])  !!}">
            </div>
            <div class="col-md-8 py-5">
                <span class="treatmentsHeadline">{!! __($value['title'])  !!}</span>
                <p class="pt-3">{!! __($value['teaser'])  !!}</p>
                <div class="pt-3">
                    <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Mehr erfahren</button>
                    <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Termin vereinbaren</button>
                </div>
            </div>
            @isset($treatments[$i]['accordion'])

                @foreach($treatments[$i]['accordion'] as $key => $acc)

                    @php
                        print_r($acc);
                    @endphp
                    <p>{!! __($acc['title']) !!}</p>
                    <p>{!! __($acc['content']) !!}</p>

                @endforeach
            @endisset
        @endforeach
        <p class="footer-vh"></p>

    </div>
</div>

相关问题