2017-01-16 47 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]; 
相关问题