2

我正在尝试让 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 数据的表单,但它的格式与此不同。我已经搜索和搜索并使用它,我能够将这些数据放入它自己的数组中,但它的代码比我想要的要多,我必须为每个名称字段和每个地址字段复制它。我觉得我必须缺少一种更简单的方法来处理名称和地址字段的这些单独部分。还是它看起来很乏味?

谢谢

4

1 回答 1

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
于 2013-03-06T18:05:02.300 回答