我正在尝试让FormStack将数据发布到我正在制作的脚本中,以便将其插入到我自己的MySQL数据库中。我能够获取数据并解析成如下所示的数组:
Array
(
[FormID] => 1111111
[UniqueID] => 222222
[name_field1] => first = FirstName last = LastName
[address_field1] => address = 123 Street Name address2 = Suite 1000 city = Exampleville state = WY zip = 12345
[random_field] => random text
[name_field2] => first = FirstName last = LastName
[address_field2] => address = 123 Street Name address2 = Suite 1000 city = Exampleville state = WY zip = 12345
)
问题在于name_fields和address_fields的格式化方式。我之前制作了包含多维数组的POST数据的表单,但格式不同于此。我已经搜索并搜索并使用它,我能够将数据转换为它自己的数组,但它的代码比我想要的多,我必须为每个名称字段和每个地址字段复制它。我觉得我必须错过一种更简单的方法来处理名称和地址字段的各个部分。或者它看起来像是单调乏味?
由于
答案 0 :(得分:0)
这可能不是最优雅的解决方案,但我很高兴我能够提出任何解决方案。这就是我想出的:
foreach ($_POST as $key => $value) {
if (preg_match("/address2 = /", $value)) {
$search = array("address2 = ", "city = ", "state = ", "zip = ");
$addr_step1 = str_replace($search, "|", $value);
$addr_step2 = str_replace("address = ", "", $addr_step1);
$addr_step3 = explode("|", $addr_step2);
$addr_keys = array("Address", "Address2", "City", "State", "Zip");
$value = array_combine($addr_keys, $addr_step3);
} elseif (preg_match("/address = /", $value)) {
$search = array("state = ", "zip = ");
$addr_step1 = str_replace($search, "|", $value);
$addr_step2 = str_replace("city = ", "| |", $addr_step1);
$addr_step3 = str_replace("address = ", "", $addr_step2);
$addr_step4 = explode("|", $addr_step3);
$addr_keys = array("Address", "Address2", "City", "State", "Zip");
$value = array_combine($addr_keys, $addr_step4);
} elseif (preg_match("/first = /", $value)) {
$name_step1 = str_replace("last = ", "|", $value);
$name_step2 = str_replace("first = ", "", $name_step1);
$name_step3 = explode("|", $name_step2);
$name_keys = array("FirstName", "LastName");
$value = array_combine($name_keys, $name_step3);
}
${'f' . $key} = $value;
}
最后一位${'f'.$key} = $value;
是给每个字段一个变量来解决它(我正在使用FormStack使用数字字段名称,这就是为什么我将'f'附加到开头)。例如,像这样......
echo "$f12345678[City], $f12345678[State]";
会告诉我......
New York, NY