动态关联数组的帖子名称

时间:2012-06-13 04:52:08

标签: php html post

我有一个房主相关的登记表。房主实际上可以拥有多个需要注册的地段。我这样做是允许用户单击一个按钮,向表单添加文本字段以允许其他数据。可以注册的额外批次数量没有限制。我的问题是如何管理帖子名称?地址,城市,州和邮政编码都需要组合在一起。基本上,我怎样才能得到这个结果:

$_POST = array(
    'mainAddress' => array(
        'address' => '',
        'city' => '',
        'state' => '',
        'zipcode' => ''
    )
    'additionalAddresses' => array(
        array(
            'address' => '',
            'city' => '',
            'state' => '',
            'zipcode' => ''
        ),
        array(
            'address' => '',
            'city' => '',
            'state' => '',
            'zipcode' => ''
        ),
        array(
            'address' => '',
            'city' => '',
            'state' => '',
            'zipcode' => ''
        )
    )
);

这甚至可能吗?如果没有,我该怎么做才能接受其他地址?

1 个答案:

答案 0 :(得分:4)

HTML:

    <form>
Address 1
    <input name="address[]" value="">
    <input name="zip[]" value="">
    <input name="city[]" value="">
    <input name="state[]" value="">

Address 2
    <input name="address[]" value="">
    <input name="zip[]" value="">
    <input name="city[]" value="">
    <input name="state[]" value="">

Address 3
    <input name="address[]" value="">
    <input name="zip[]" value="">
    <input name="city[]" value="">
    <input name="state[]" value="">
    </form>

PHP代码:

$new_address_array = array();
foreach($_POST['address'] as $k=>$v){
$temparray['address'] = $v;
$temparray['city'] = $_POST['city'][$k]; 
$temparray['state'] = $_POST['state'][$k]; 
$temparray['zip'] = $_POST['zip'][$k]; 
$new_address_array[] = $temparray;
}

$final['addtional_address']=$new_address_array;

print_r($final);