开发者

how to process a complex php array submitted by a form?

i have this array submitted using an extjs form:

$submittedFormaddress = array('city' => array('london', 'boston', 'paris'),
'country' => array('UK', 'USA', 'FRANCE'),
'zip' => array('22222', '33333', '44444'));

How can i automatically create below array from $submittedFormaddress?

$clientAddress=array('address1'=>array
('city'=>'london','country'=>'UK','zip'=>'2222'),'address2'=>array
('city'=>'bos开发者_JS百科ton','country'=>'USA','zip'=>'3333'),'address3'=>array
('city'=>'paris','country'=>'FRANCE','zip'=>'44444'));


I would suggest using foreach() to loop through and drill down.

I tried to make each variable name descriptive:

<?php
foreach ($submittedFormaddress as $address_part => $part_array) {
    foreach($part_array as $index => $name) {
        $clientAddress['address' . ($index + 1)][$address_part] = $name;
    }
}
?>

Working Example


for ($i = 0; $i < count($submittedFormaddress['city']); $i++)
{
    $clientAddress['address'.$i] = array(
        'city' => $submittedFormaddress['city'][$i],
        'country' => $submittedFormaddress['country'][$i],
        'zip' => $submittedFormaddress['zip'][$i],
    );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜