Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

m7vm7v's avatar
Level 51

How to restructure an array properly?

Could you possibly give me an advise?

I need my data to be restructured back into order as it was.

I have a controller that feed a view with array

return [
            'active' => true,
            'items'  => [
                [
                    'info' => 'Phone',
                    'icon' => 'fa fa-phone',
                ],
            ],
            'button' => [
                'link' => 'www.google.com',
                'text' => 'Get Started',
            ],
        ];

thats is being rendered in view called partials.blade.php

@foreach($items as $root => $item)
    <div class="control-group">
        <label class="control-label">{{($root) }}</label>

        @if(is_array($item))
            <div>
                @include('partials', ['items' => $item])
            </div>
        @else
            <div class="controls">
                <input type="text" name="{{ $root }}" value="{{ $item }}" class="span12">
            </div>
        @endif

    </div>
@endforeach

As you see I am calling the view recursively. Visually I see everything as it need to be seen. What the issue is that the name="{{ $root }}" is always only the last instance when I need to get all the data structured the same way as it was.

To visualise this - when I post the data I receive -

array:6 [
  "active" => "1"
  "info" => "<span>New York, USA</span>"
  "icon" => "fa fa-map-marker"
  "link" => "www.google.com"
  "text" => "Get Started"
]

And its obvious why is it so but what I need is an advise what would be the best way to collect all the data and to receive it in the same way I've send it.

0 likes
0 replies

Please or to participate in this conversation.