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

shariff's avatar
Level 51

how to define nested array input fields names

Hi all,

I'm creating a form where user can add or remove input fields. Every input is an array field. I have a situation where one of the field is an array inside another array. I'm not understanding how to handle this situation.

one row has 2 fields 
<div id="each-row">
    <input value="" class="form-control" type="text"  name="size[]" placeholder="" required="">
    @foreach($items as $item)
	<div>
         <label>
             <input type="checkbox" name="colors[][color]" value="{{$item->id}}">
               {{$item->name}}
        </label>
    </div>
    @endforeach
</div>

the div is added and removed using jquery just for example I showed. I want to know how to give name for colors checkbox

0 likes
1 reply
kldio's avatar

If i understand correctly you want to do something like this

<div id="each-row">
    @foreach($items as $index => $item)
    <div>
        <input value="" class="form-control" type="text" name="size[{{$index}}]" placeholder="" required="">
        <label>
            <input type="checkbox" name="colors[{{$index}}][color]" value="{{$item->id}}">
            {{$item->name}}
        </label>
    </div>
    @endforeach
</div>
1 like

Please or to participate in this conversation.