在循环中更改变量名称
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;
}
我有超过50个字符串。 我需要将这些字符串放入一个数组中。 我尝试使用循环来创建它。 但是我在自动增量var的名字上遇到了麻烦。 $ string。$ s code>(
$ string1,$ string2 code>)。 它将成为Undefined变量,是否可以更改var的名称? p>
$ string0 =“ABC”;
$ string1 =“DEF”;
$ string2 =“GHI “;
...
$ data = array($ string0,$ string1,$ string2 ...);
for($ s = 0; $ s&lt; 50; $ s ++){
$ data [$ s] = $ string。$ s;
}
code> pre>
div>
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)
Points:
- Search by
(\$string)([0-9]+)(=)
- Replace to
\1[\2]\3
- Mode Regex
something like this should work I guess :
for($s=0; $s<50; $s++)
{
$myvar = "string" . $s;
$data[$s]=$$myvar;
}