【问题标题】:How combine one string and variable for new variable如何将一个字符串和变量组合为新变量
【发布时间】:2014-10-28 13:32:22
【问题描述】:

$i18ncoches="coches"; $i18nmotos="motos";

您好,我正在尝试从 foreach 创建一个变量,结合一个字符串和一个变量

$apartados=array('coches', 'motos', 'naves', 'avion');

foreach ($apartados as $key) {
    echo $i18n.$key;

}

但预期的结果是留下以下

$i18ncoches
$i18nmotos

但得到以下 注意:未定义变量:i18n

coches motos etc,,,

【问题讨论】:

    标签: php variables foreach set


    【解决方案1】:

    你可以这样做:

    <?php
    
        $i18ncoches="coches";
        $i18nmotos="motos";
    
        $apartados=array('coches', 'motos', 'naves', 'avion');
    
        foreach ($apartados as $key) {
    
           // if(isset(${'i18n'.$key})) //you will need this check 
                 echo ${'i18n'.$key};
    
        }
    
    ?>
    

    【讨论】:

    • 谢谢,并试图寻找解决方案,这就是所寻求的解决方案
    猜你喜欢
    • 2018-03-03
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2018-10-23
    • 2015-07-29
    相关资源
    最近更新 更多