Home > Software design >  Echo a multidimensional PHP array in Laravel
Echo a multidimensional PHP array in Laravel

Time:12-01

I want to return my nested array in a foreach loop. In this foreach loop I need to add a foreach, because it is variable how many accordions exist.

But I can't get it to load the data from the "accordion" array into the frontend. There is always an error somewhere.

I can't describe it any better right now.

Save

This is my treatments.php file:

'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',
                ],
            ],
        ],
    ]

And this is my Frontend Blade Code:

<div >
        @foreach( $treatments as $key => $value)
            <?php print_r($value) ?>
            <div >
                <img  src="{!! __($value['img']) !!}" alt="{!! __($value['title'])  !!}">
            </div>
            <div >
                <span >{!! __($value['title'])  !!}</span>
                <p >{!! __($value['teaser'])  !!}</p>
                <div >
                    <button  href="#{!! __($value['pop-up']) !!}">Mehr erfahren</button>
                    <button  href="#{!! __($value['pop-up']) !!}">Termin vereinbaren</button>
                </div>
            </div>
                @foreach($value as $acc)
                    <p>{!! __($acc['title']) !!}</p>
                @endforeach
        @endforeach
        <p ></p>

    </div>

And this is the Array printed out: enter image description here

CodePudding user response:

@foreach($value as $acc) should be @foreach($value['accordion'] as $acc)

CodePudding user response:

I finaly got it!

@php
    $i = -1;
@endphp
<div >
    <img  src="{!! __('treatments.derma') !!}" alt="">
</div>
<div >
    <div >
        @foreach( $treatments as $key => $value)
            @php
                $i  ;
            @endphp

            <div >
                <img  src="{!! __($value['img']) !!}" alt="{!! __($value['title'])  !!}">
            </div>
            <div >
                <span >{!! __($value['title'])  !!}</span>
                <p >{!! __($value['teaser'])  !!}</p>
                <div >
                    <button  href="#{!! __($value['pop-up']) !!}">Mehr erfahren</button>
                    <button  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 ></p>

    </div>
</div>
  • Related