2010-02-15 45 views
2

如何分配的名字,我有數組每一個關鍵

$fullname = $_POST['fullname']; 


$company_name = $_POST['company_name']; 


$email = $_POST['email']; 


$address = $_POST['address']; 


$telephone = $_POST['telephone']; 


$comment = $_POST['comment']; 


$data = array ( "Full Name :" => $fullname, 
       "Company :" => $company_name, 
       "email :" => $email, 
       "address :" => $address, 
       "telephone :" => $telephone, 
       "mobile :" => $mobile, 
       "comment :" => $comment); 

我想表明resul如(Fullname : $fullname Email: $email)使用implode(" ",$data)

我能做些什麼,以它作爲電子郵件發送

和字符串添加數組??

回答

2
$fields = array('fullname' => 'Full Name', 'company_name' => 'Company', 'email'=>'Email', 'address' => 'Address', 'telephone'=>'Telephone', 'comment'=>'Comment'); 

$string = "("; 
foreach($fields as $field_name => $field_label) 
{ 
    if(isset($_POST[$field_name])) 
    { 
     $string .= $field_label .' :' . $_POST[$field_name] . ' '; 
    } 
} 
$string .= ')'; 
+0

謝謝你很好的答案,它的工作 –

1

有地圖的$ _ POST鍵映射的各自的標籤:

$arr = array('fullname' => 'Fullname:'); // etc 

$data = array(); 
foreach($_POST as $k => $v) { 
    if(array_key_exists($k, $arr)) { 
     $data[$arr[$k]] = $v; 
    } 
} 

我只想用var_export()打印出來的電子郵件:

var_export()獲取結構 有關給定變量的信息。 它與var_dump()類似,但有一個 例外:返回的表示 是有效的PHP代碼。

所以:

$output = var_export($data, true); 
// $output goes into your email