dsgfdgh14569 2013-05-15 12:30
浏览 36
已采纳

在循环中更改变量名称

I have more than 50 string. I need to put those string into one array. I try to use loop to create it. however I have trouble on auto increment var's name. $string.$s($string1, $string2). It will become Undefined variable, Any way to change var's name?

$string0="ABC";
$string1="DEF";
$string2="GHI";
...


$data=array($string0, $string1, $string2...);

for($s=0; $s<50; $s++){
    $data[$s]=$string.$s;
}
  • 写回答

2条回答 默认 最新

  • douhai5835 2013-05-15 12:32
    关注

    Not

    $string.$s;
    

    But

    ${'string'.$s};
    

    Needless to say, the best solution is:

    $data = array(
        'ABC',
        'DEF',
        'GHI',
    );
    

    or

    $data = array();
    $data[] = 'ABC';
    $data[] = 'DEF';
    $data[] = 'GHI';
    

    or

    $data = array();
    $data[0] = 'ABC';
    $data[1] = 'DEF';
    $data[2] = 'GHI';
    

    You can also replace them by your editor's function.

    I'll take one example, Notepad++. (Japanese plug-in is set on, sorry)

    Let's try like this: enter image description here

    Points:

    • Search by (\$string)([0-9]+)(=)
    • Replace to \1[\2]\3
    • Mode Regex
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog