我正在使用一个新的支付处理器,他们的数据表只是说他们的信息以多维数组的形式发布。仅引用几个变量:
products[x][prod_number]
products[x][prod_name]
products[x][prod_type]
** 有十个这样的数组
我发现如果这个人订购了 3 件商品,就会有一个名为"item_count"的变量,这意味着 X 变为 [0]、[1] 和 [2]
但是读取此 POST 数据并将其分离的方法是什么?当然,这将是某种"foreach"循环,但是我需要什么变量名称是一个谜
对于普通变量,即获取"名称/值"对,我使用这个:
use CGI qw/:standard/;
@names=param;
foreach $name(@names){
$value=param($name);
$$name=$value;
}
有什么指示吗?
+++++
不确定这是否是添加到这篇文章的正确方法;我还在学习系统
我现在的问题是这些数据发布到 STDIN 等的格式是什么。或者更切中要害,它会被读成什么。由于"产品"是单个变量名称,因此所有数据都在单个"$products"变量中,还是所有数据都包含在@products中?
my $num_items = $cgi->param('item_count');
my @ordered_prods;
for my $ordered_prod_num (1..$num_items) {
my %ordered_prod;
for my $field_name (qw( prod_number prod_name prod_type )) {
$ordered_prod{$field_name} =
$cgi->param("products[$ordered_prod_num][$field_name]");
}
push @ordered_prods, %ordered_prod;
}
或者转念一想,
my @ordered_prods;
for my $param_name ($cgi->param()) {
if (
my ($ordered_prod_num, $field_name) =
$param_name =~ /^products[([0-9]+)][(w+)]z/
) {
$ordered_prods[$ordered_prod_num]{$field_name} =
$cgi->param($param_name);
}
}
接收 CGI 参数的更好方法:
哈希参考:
my $params = { map { $_ => ($cgi->param($_))[0] } $cgi->param };
直接哈希:
my %params = map { $_ => ($cgi->param($_))[0] } $cgi->param;
或僧侣模式;)
sub Vars {
my $q = shift;
my %in;
tie(%in,CGI,$q);
return %in if wantarray;
return %in;
}
我设法通过开发一个其他人可能喜欢尝试的小脚本来解决多维数组的工作原理。(我可以离线测试,因为我在 Windows 的 PC 上运行了一个 PERL。毫无疑问,其他人可以通过类似的环境进行测试)
基本上,我从另一张海报中复制了一个关于如何 POST 数组的"问题",并构建了一个脚本来分析从 HTML 表单发送到服务器脚本的内容。然后,我在上面发布的答案的帮助下添加了自己的解码例程。就"纯"编程而言,它可能很粗糙,但可以分阶段研究以查看正在发生的事情:(我更喜欢的工作方式!
#!/usr/bin/perl
use CGI qw/:standard/;
print "content-type: text/htmlnn";
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
}
希望这可以帮助其他人了解数组的工作原理。
注意:似乎多维数组是完整发送的,即: