在PHP中接收多维文本框数组

I have text box values to be posted . How do I take it in a PHP array .The html is as follows:

<input type="text" name="ItemName[1][2]" >
<input type="text" name="ItemName[1][3]" >
<input type="text" name="ItemName[1][4]" >

$ItemNamesArray = $_POST[] ..... ????? What do I do in this step???

Please help.

$_POST['ItemName'][1][2];
$_POST['ItemName'][1][3];
$_POST['ItemName'][1][4];

If you use var_dump($_POST), you'd see the answer right away:

array(1) {
  ["ItemName"]=>
  array(1) {
    [1]=>
    array(3) {
      [2]=>
      string(1) "a"
      [3]=>
      string(1) "b"
      [4]=>
      string(1) "c"
    }
  }
}

So you access $_POST['ItemName'][1][2] for example.

The post array can be accessed like $ItemNamesArray = $_POST['ItemName']

and you can access the first item $ItemNamesArray[1][2]

Well, in your example:

<input type="text" name="ItemName[1][2]" >
<input type="text" name="ItemName[1][3]" >
<input type="text" name="ItemName[1][4]" >

The $_POST key is going to be the name, so ItemName and because PHP treats the follow-up brackets as array keys, the values would be:

$_POST['ItemName'][1][2]
$_POST['ItemName'][1][3]
$_POST['ItemName'][1][4]

So if you want all item names, go with:

$itemNames = $_POST['ItemName'];

FYI,

  1. you should sanitize and validate all input before using it.

  2. If you don't actually have an ItemName[2][1] or ItemName[0][1] etc, then the following would make more sense:

     <input type="text" name="ItemName[]" />
     <input type="text" name="ItemName[]" />
     <input type="text" name="ItemName[]" />
    

Which would just set the index as they came in, instead of pre-setting them.