如何为每个键指定名称

时间:2010-02-15 15:59:56

标签: php

我有阵列

$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);

我希望显示结果,例如(Fullname : $fullname Email: $email)将其作为电子邮件发送

并使用implode(" ",$data)

在字符串中添加数组

我该怎么办?

2 个答案:

答案 0 :(得分: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 .= ')';

答案 1 :(得分: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()类似   exception:返回的表示形式   是有效的PHP代码。

所以:

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