如何在数组值+ foreach中调用数组

时间:2012-08-27 10:26:28

标签: php arrays loops foreach

我有一个联系人,可以将电子邮件发送给不同的人作为下拉选项。

选项值在带有数组的数组中定义,我正在努力调用数组值,以便电子邮件可以转到指定人员。

此处定义了数组:

if((isset($_GET['enquiry']) && $_GET['enquiry']=='Locate a dealer/distributor')||(isset($_POST['enquiry']) && $_POST['enquiry']=='Locate a dealer/distributor')){
    $chk_Locate=TRUE;
}

if(isset($_GET['enquiry']) && $_GET['enquiry'] == 'CC'){
    $query_recipients = array(array('Complaint', 'csm@examplecompany.co.za'),
                            array('Suggestion', 'csm@examplecompany.co.za'),
                            array('Compliment', 'csm@examplecompany.co.za'));
    $enquiry = '<input type="hidden" name="enquiry" id="enquiry" value="CC">';
}

else{
    $query_recipients = array(array('Locate a dealer/distributor', 'sales@examplecompany.co.za'),
                            array('Technical support', 'technical@examplecompany.co.za'),
                            array('Back orders', 'backorders@examplecompany.co.za'),
                            array('Product enquiry', 'sales@examplecompany.co.za'),
                            array('Catalog request', 'sales@examplecompany.co.za'),
                            array('New customer enquiry', 'sales@examplecompany.co.za'),
                            array('Existing customers - logon', 'dealers@examplecompany.co.za'),
                            array('Existing customers - orders', 'weborder@examplecompany.co.za'),
                            array('Report web problems', 'webmaster@examplecompany.co.za'));
}

循环

foreach($query_recipients as $key => $val){ 
    if((isset($_POST['enquiry']) && $_POST['enquiry'] == $val[0])||(isset($_GET['enquiry']) && $_GET['enquiry'] == $val[0])){
        $selected = $val[0].' : '.$item;
        if($val[0]=='Existing customers - logon'||$val[0]=='Technical support'||$val[0]=='Catalog request'||$val[0]=='Product enquiry'||$val[0]=='New customer enquiry'||$val[0]=='Existing customers - orders'||$val[0]=='Catalog request')

调用它们的代码是:

$_POST['recipient_email'] = $contact_email.','.$recpt_email;
    if(isset($ref_number)){
        $_POST['subject'] = 'Examplecompany Group: '.$ref_number.': '.$_POST['enquiry'];
    }else{
        $_POST['subject'] = 'Examplecompany Group: '.$_POST['enquiry'];
    }
    $_POST['sender_email'] = $_POST['email'];
    $_POST['cc'] = $_POST['email'].', Examplecompany Group <Examplecompany.orders@gmail.com>';
    $_POST['body'] = '<p>Name: ' . $_POST['name'] . '</p>'
                        . '<p>Surname: ' . $_POST['surname'] . '</p>'
                        . '<p>Company: ' . $_POST['company'] . '</p>'
                        . '<p>Examplecompany Account No.: ' . $_POST['accnr'] . '</p>'
                        . '<p>Account No. verification: ' . $cust_verify . '</p>'
                        . '<p>Email: ' . $_POST['email'] . '</p>'
                        . '<p>Tel/cell: ' . $_POST['tel'] . '</p>'
                        . $_POST['enquiry'] . ': ' . $_POST['message'];

    if(Email::Send($_POST, NULL)){
        $message = 'Your feedback has been sent'.$cont_ref;
    } else {
        $message = 'There was an error sending your feedback';
    }
}

这是我想用来调用它们$_POST['recipient_email'] = $contact_email.','.$recpt_email;的行,因为它现在可以正常工作,但无论选择何种选项,它都会调用最后一个数组array('Report web problems', 'webmaster@examplecompany.co.za')

请帮忙,如果我能找到一种方法来调用数组值,我相信我的表单会按照计划进行工作。

有一条我不理解的线

$enquiry_options .= '<option>'.$val[0].'</option>';
    $recpt_email = $val[1];

如果val设置为$Val[1],则表单会将电子邮件发送到阵列上的最后一个电子邮件地址,但如果设置为$Val[0]则不会。

1 个答案:

答案 0 :(得分:0)

考虑将数组切换为使用键值对而不是嵌套数组。 (请参阅array文档。)

$enquiry_choices = array(
    'Complaint' => 'csm@examplecompany.co.za',
    'Suggestion' => 'csm@examplecompany.co.za',
    'Compliment' => 'csm@examplecompany.co.za',
    'Locate a dealer/distributor' => 'sales@examplecompany.co.za',
    'Technical support' => 'technical@examplecompany.co.za',
    'Back orders' => 'backorders@examplecompany.co.za',
    'Product enquiry' => 'sales@examplecompany.co.za',
    'Catalog request' => 'sales@examplecompany.co.za',
    'New customer enquiry' => 'sales@examplecompany.co.za',
    'Existing customers - logon' => 'dealers@examplecompany.co.za',
    'Existing customers - orders' => 'weborder@examplecompany.co.za',
    'Web site problems' => 'webmaster@examplecompany.co.za',
);

// Optional: Sort the choices by name
ksort($enquiry_choices);

这样可以轻松获取与用户选择相关联的电子邮件地址。

$enquiry = $_GET['enquiry'];
$enquiry_email = (array_key_exists($enquiry, $enquiry_choices) ? $enquiry_choices[$enquiry] : false);

为下拉列表生成HTML也很简单。 (请参阅foreach文档。)请注意,定义$enquiry的上述代码必须位于以下代码之前。

echo <<<HTML
<div>
    <label for="enquiry">Enquiry:</label>
</div>
<select name="enquiry" id="enquiry" class="select">
HTML;

foreach($enquiry_choices as $key => $value) {
    $selected = ($enquiry == $key ? ' selected="selected"' : '');
    echo "<option$selected>$key</option>";
}

echo '</select>';