我有一个关联数组,其中我将字符串绑定到一个变量。有没有一种简单的方法可以将变量增加1?
$lang = array(
'All Articles' => $t0,
'Main Articles' => $t1,
'Archived Articles' => $t2,
'Search Articles' => $t3,
'Search for' => $t4,
'Page' => $t5,
'from' => $t6,
// and so on...
);
因此,我正在寻找类似的东西,为每个示例创建变量$t0
到$t160
。
我试过这个,但不起作用:
$i = 0;
$lang = array(
'All Articles' => $t.$i++,
'Main Articles' => $t.$i++,
'Archived Articles' => $t.$i++,
'Search Articles' => $t.$i++,
'Search for' => $t.$i++,
'Page' => $t.$i++,
'from' => $t.$i++,
用于哪里:
管理员通过填写表单将翻译后的字符串存储到.txt
文件中。一个txt文件如下所示:
Alle Produkte
Hauptartikel
Archivierte Artikel
// and so on
然后读取文本文件的内容:
$translationfile = 'data/translations.txt';
$lines_translationfile = file($translationfile, FILE_IGNORE_NEW_LINES); // all lines of the translations.txt file into an array
for ($x = 0; $x <= 160; $x++) {
${"t".$x} = $lines_translationfile[$x];
}
include 'includes/lang.php'; // the associative array
现在在页面中,我可以很容易地用$lang['All Articles']
翻译字符串
只需创建一个键数组,就可以使用array_combine
将它们与文件中的行直接组合:
$translationfile = 'data/translations.txt';
$lines_translationfile = file($translationfile, FILE_IGNORE_NEW_LINES); // all lines of the translations.txt file into an array
$keys = array('All Articles','Main Articles','Archived Articles','Search Articles','Search for','Page','from', ...);
$lang = array_combine($keys, $lines_translationfile);
试试这个:
$i = 0;
$lang = array(
'All Articles' => ${"t".$i++},
'Main Articles' => ${"t".$i++},
'Archived Articles' => ${"t".$i++},
'Search Articles' => ${"t".$i++},
'Search for' => ${"t".$i++},
'Page' => ${"t".$i++},
'from' => ${"t".$i++});