views:

474

answers:

2

i want to know how i can post a multi-dimensional array?

basically i want to select a user and selected user will have email and name to sent to post.

so selecting 100 users, will have email and name.. i want to get in php like following

$_POST['users'] = array(array(name, email), array(name2, email2), array(name3, email3));

any idea?

+4  A: 

You can name your form elements like this:

<input name="users[1][name]" />
<input name="users[1][email]" />
<input name="users[2][name]" />
<input name="users[2][email]" />
...

You get the idea...

Franz
what about users[][name], do i have to set the id (1, 2..)?
Basit
Nope. You can go with `users[]`, too.
Franz
+1  A: 

Well, you are going to have to do some looping somewhere. If you name each form element with an index (as Franz suggests), you do the looping on the PHP side.

If you want to use Javascript to do the looping, have your form onSubmit() create a JSON string to pass to the PHP. Then have the PHP retrieve it like so:

json_decode($_POST['users'], true);

The second argument tells it to make arrays instead of anonymous objects.

Benjamin Cox