8

I understand the basics of using the array-formatted HTML input names. If I had a form with a variable number of 'item' inputs I might do something like this for each of them:

<input name='item[]' type='text' />

And when I retrieve the items from the $_POST array I could iterate over them like so:

$items = $_POST['item'];
foreach($items as $item) {
}

But my question is slightly more complicated. I have a form where users can click a button "add one" and a new row will appear at the bottom number of the form. Each new row contains a 'name' and 'description' input.

So initially I thought I would do this:

<input name='item[name][]' type='text' />
<input name='item[description][]' type='text' />

And then iterate over them like so:

$items = $_POST['item'];
foreach($items as $item) {
  print $item['name'] . ' ' . $item['description'];
}

But instead of working as I hoped, it instead structures the 'item' array such that I would access the first item name as $item['name'][0] rather than as $item[0]['name'].

So then I flipped it so that my inputs were named as such:

<input name='item[][name]' type='text' />
<input name='item[][description]' type='text' />

But this resulted in a separate 'item' for each 'name' and for each 'description' rather than grouping each pair in a single 'item'.

I really dislike having arrays of 'name' and separate array of 'description'. I would prefer arrays of 'item' with each array containing a 'name' and a 'description' field. Is there any way to accomplish this without generating the an index in my javascript? Since people can add and remove these dynamically it's very difficult for my javascript to calculate the appropriate index for the next item. Is there no way to do this generically?

3
  • What's so bad about having an array of name and an array of description? -- you can piece them back together later. Commented Jan 17, 2011 at 4:00
  • 2
    @Mark E: I have obsessive compulsive disorder and this really, really bugs me to the point where I am unable to just hack my way around it and proceed. All my other arrays (from database for example) are row index as the first dimension and property name as the second. Commented Jan 17, 2011 at 4:04
  • 2
    Well the only way to overcome your naming problem is to put numbers in the empty brackets, that seems to be the worst of the available choices, don't you think? Commented Jan 17, 2011 at 4:08

2 Answers 2

9

It's not possible to do what you want, but if it helps, here's some code to piece it back together that I think will work (with item_name[] and item_description[]):

$items_desc = $_POST["item_description"];
$items_name = $_POST["item_name"];
$items = array();
for ($i = 0; $i < count($items_name); $i++)
{
    $items[] = array("name" => $items_name[$i], "description" => $items_desc[$i]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

used the format button on your code block cause it wasnt coming through properly with you pre tags. hope you dont mind.
@prodigitalson this is how moderators should act/behave! I point it out for others to recognize!
0

You have to give the index a value:

<input name='item[0][name]' type='text' />
<input name='item[0][description]' type='text' />

<input name='item[1][name]' type='text' />
<input name='item[1][description]' type='text' />

...

<input name='item[n][name]' type='text' />
<input name='item[n][description]' type='text' />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.