当前位置: 首页 > 工具软件 > Analog PHP > 使用案例 >

php _post 二维数组,post 提交二维数组_通过POST将多维数组从表单提交到PHP

况弘新
2023-12-01

post 提交二维数组

After much searching on the subject myself, I've found that many people have wondered whether it's possible to submit a multidimensional array from an HTML form to a PHP script.  And for some reason, all the articles answering this query are convoluted or wrong.  The answer is simple: YES!

在我自己对该主题进行了大量搜索之后,我发现许多人想知道是否可以从HTML表单向PHP脚本提交多维数组。 出于某种原因,所有回答此查询的文章都是令人费解或错误的。 答案很简单:是的!

It is not only possible, but very straight-forward to submit a multi-dimensional array from a form to a PHP script.  The functionality was added in PHP 4 (and nobody but... well nobody... should still be using a version of PHP prior to 4).   Here is how it works:

从表单向PHP脚本提交多维数组不仅可行,而且非常简单。 该功能是在PHP 4中添加的(没有人,但是……好吧……应该仍然使用4之前PHP版本)。 下面是它的工作原理:

Let's say you want to let someone pick several "options" to make available for a new product on your web page, so you have some check boxes for colors, sizes and types.  But you have lots of other data in your form you don't want to get it confused with (perhaps there's another "type" of something else also in your form), so you want to contain all of these selections in a sub array called "options" so you can find them easily once you get to the PHP.  Here's how that would look in XHTML:

假设您想让某人选择几个“选项”以使新产品在您的网页上可用,因此您有一些复选框可以选择颜色,大小和类型。 但是您不想在表单中混淆其他数据(也许您的表单中还有其他“类型”的东西),因此您希望将所有这些选择都包含在一个称为“子数组”的子数组中“选项”,因此一旦进入PHP,您就可以轻松找到它们。 这是在XHTML中的外观:

...

check which options this product will be available in:

SIZES:

small

medium

large

COLORS:

red

blue

green

orange

TYPES

floppy

compact

hard

digital

analog

...

And as long as you're running a version of PHP that is not ancient, your $_POST array will look something like this (depending on options checked).:

只要您运行的不是旧版本PHP,您的$ _POST数组就会看起来像这样(取决于选中的选项):

Array

(

...

[options] => Array

(

[sizes] => Array

(

[0] => small

[1] => large

)

[colors] => Array

(

[0] => red

[1] => green

)

[types] => Array

(

[0] => floppy

[1] => analog

[2] => digital

)

)

...

)

Pretty much how you'd expect it to look.  Nothin' tricky about it!  So don't worry, go ahead and use them; they work!

您几乎可以期待它的外观。 没什么棘手的! 因此,不用担心,继续使用它们。 他们工作!

翻译自: https://www.experts-exchange.com/articles/2453/Submitting-a-multidimensional-array-from-a-form-to-PHP-via-POST.html

post 提交二维数组

 类似资料: