i almost everytime use {{ Form::text("foo[$index]", $thing->foo) }} seems to me better fit for every situation. Easier to loop through using indexes and easier to access each item on any level without a need to concatenate variable names . So even with multidimensional arrays i can access them $input[$i][$j] . But everyone is different i guess.
Nov 22, 2014
1
Level 13
Convention for posting arrays to php
I'm curious how people choose to post form arrays to php. For example if you had a few form elements you could do:
@foreach($things as $index => $thing)
{{ Form::text("foo[$index]", $thing->foo) }}
{{ Form::text("bar[$index]", $thing->bar) }}
@endforeach
which produces this in the POST:
array (size=2)
'foo' =>
array (size=6)
1 => string '' (length=0)
2 => string '' (length=0)
'bar' =>
array (size=6)
1 => string '' (length=0)
2 => string '' (length=0)
Or you could do:
@foreach($things as $index => $thing)
{{ Form::text($index."[foo]", $thing->foo) }}
{{ Form::text($index."[bar]", $thing->bar) }}
@endforeach
giving this in the post
array (size=2)
1 =>
array (size=2)
'foo' => string '' (length=0)
'bar' => string '' (length=0)
2 =>
array (size=2)
'foo' => string '' (length=0)
'bar' => string '' (length=0)
There are advantages in both ways. What do you use? I know this is a bit more than just a php question but has the PHP-FIG ever addressed this? Is there even a convention for this?
Please or to participate in this conversation.