2017-01-16 46 views
-1

我需要編寫一個代碼,允許我發送一個數組中的特定鏈接。這是我想要做的一個簡短的想法。根據國家代碼,我會用特定的語言發送小冊子。我也想知道如果我可以通過開關做到這一點...如何發送數組中的特定鏈接?

這是我到目前爲止的代碼...

<?php 
$de_brochure = ('https://ruta/de-brochure.pdf'); 
$en_brochure = ('https://ruta/en-brochure.pdf'); 
$es_brochure = ('https://ruta/es-brochure.pdf'); 
$country_code = 'ES'; // Normally I get this code from a form. 
$brochure = array ($de_brochure, $en_brochure, $es_brochure); 
$brochure_link = ''; 

if ($country_code == 'ES') { 
    $to = '[email protected]'; 
    $subject = 'Ejemplo'; 
    $txt = 'El dossier a enviar es' . $brochure_link[$brochure]; 
    $headers = 'De: [email protected]' . '\r\n' . 
'CC: [email protected]'; 
    mail ($to, $subject, $txt, $headers); 
} else { 
    echo $country_code . 'no es el código de españa'; 
} 

當我運行我的代碼,這是我得到的輸出:

警告非法偏移類型上的行號17

注意未初始化的字符串偏移量:行號1 17

+2

好,'$ brochure_link'是一個字符串,而不是一個數組,所以'$ brochure_link [$ brochure]'會引發錯誤。 – roberto06

+0

你期望什麼?沒有數組'$ brochure_link'索引'$ brochure_link [$ brochure]' – C2486

+0

如果我知道該怎麼做我瘦我不會問@Rishi謝謝你這麼有禮貌。這只是一個想法,我想要幫助解決這個問題。 – KAZZABE

回答

1

你讓你的陣列和一個未使用的「鏈接」變量

$brochure = array ($de_brochure, $en_brochure, $es_brochure); 
$brochure_link = ''; 

,然後訪問而非陣列此鏈接變量:

$txt = 'El dossier a enviar es' . $brochure_link[$brochure]; 
            ^^^^^^^^^^^^^^^^^^^^^^^^^ 

這是它失敗。使用數組名爲鍵(即哈希)會更容易:

$brochures = [ 
    'DE' => 'https://ruta/de-brochure.pdf', 
    'EN' => 'https://ruta/en-brochure.pdf', 
    'ES' => 'https://ruta/es-brochure.pdf' 
]; 

$country_code = 'ES'; 

# ... 

$txt = 'El dossier a enviar es' . $brochures[$country_code]; 
+0

謝謝@sidyll我會做這些修復,並嘗試再次運行它,看看它是如何發展的。謝謝! – KAZZABE

+0

謝謝!我按照解釋的方式使用了這段代碼,它工作得很好。非常感謝! @sidyll – KAZZABE

+0

樂於幫助@KAZZABE! – sidyll

0

做這樣的事情

$brochure_link_arr= array(
"DE"=>'https://ruta/de-brochure.pdf', 
"EN" =>'https://ruta/en-brochure.pdf', 
"ES"=> 'https://ruta/es-brochure.pdf' 
); 

if ($country_code == 'ES') { 
.. 
$txt = 'El dossier a enviar es' . $brochure_link_arr[$country_code]; 
相關問題