通过开发其他人可能想尝试的小脚本,我设法解决了多维数组的工作原理。(我可以离线测试这个,因为我在我的 Window 的 PC 上运行了一个 PERL。毫无疑问,其他人可以通过类似的环境进行测试)
基本上,我从另一张海报中复制了一个关于如何发布数组的“问题”,并构建了一个脚本来分析从 HTML 表单发送到服务器脚本的内容。然后,我在上面发布的答案的帮助下添加了自己的解码程序。就“纯”编程而言,它可能很粗糙,但可以逐步研究它,看看发生了什么:(我更喜欢的工作方式!)
#!/usr/bin/perl
use CGI qw/:standard/;
print "content-type: text/html\n\n";
use CGI::Carp qw( fatalsToBrowser );
if (param('action') eq '1'){ # Ignore this: Simply to help demo work
&output;
}
## The form window
## Allows two people to input required "diameters"
print <<EOF;
<form method="post" action="http://www.MY-DOMAIN.com/cgi-bin/test_multi.pl"> <!--Post to your own domain-->
<input type="hidden" name="action" value="1"> <!-- Ignore: Simply helps demo script along-->
<table>
<tr>
<td>Customer 0: Top diameter<input name="diameter[0][top]" type="text" size="5"></td>
<td>Customer 0:Bottom diameter<input name="diameter[0][bottom]" type="text" size="5"></td>
</tr>
<tr>
<td>Customer 1: Top diameter<input name="diameter[1][top]" type="text" size="5"></td>
<td>Customer 1: Bottom diameter<input name="diameter[1][bottom]" type="text" size="5"></td>
</tr>
</table>
<input type="submit" value="send form">
</form>
EOF
sub output{ # Ignore fact that it's in sub-routine: Just makes demo easier
print "These are the \"name/value\" pairs as seen by the input to script,<br>ie what form sends to <stdin><br><b>Note: The \"|\" symbol is added by script for clarity</b><br><br>";
@names=param;
foreach $name(@names){
$value=param($name);
print "$name=$value|"; #Visual reminder of what's happening
}
print "<hr>";
$item_count=1; # ONE LESS than less than total number of "levels" in HTML form, ie Customer[0], Customer[1] = 1
@field_name=('top','bottom'); # Fields as defined in form
foreach $i($item_count){ # Loop through each of the levels, (Customer[0] and Customer[1] in this example)
foreach $x(@field_name){ # Loop through the fields within each level ([top] and [bottom] in example)
$name=$field_name[$x]; # Places the field name[$x] in the variable "name"
$value=param("diameter[$i][$field_name[$x]]"); # The value of the array is assigned to a variable "value"
$$name=$value; # The name is assigned to a variable, and given the value (ie "$top=xx" "$bottom=zz")
print "Customer $i \$$name=".$value."<br>"; # Visual reminder of what's happening
}
# Process this loop, and do something with data before moving to next loop
# Values on first loop: "$i = 0", "$top=xx" and "$bottom=zz" with values of the first array
# Value on second loop: "$i=1", "$top=xx" and "$bottom=zz" with values of the second array
}
print "<hr>";
# The names of "diameter", "top" and "bottom" reflect the names used in the form and would be changed to suit requirements.
# The advantage of this script is it assigns values back to single variables,
# and avoiding getting in a muddle trying to keep track of which element of [A][B] array one is working on at any time
}
希望这可以帮助其他人了解阵列的工作原理。
注意:似乎多维数组是完整发送的,即:diameter[0][top]=x&diameter[0][bottom]=z&diameter[1][top]=x&diameter[1][bottom]=z,带有正方形成对的括号