如何在 php 中将变量名称递增 1
how to increment variable name by 1 in php
我有一个关联数组,我在其中将一个字符串绑定到一个变量。
有没有一种简单的方法可以将变量递增 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']
的字符串
试试这个:
$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++});
只需创建一个键数组,然后您可以使用 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);
我有一个关联数组,我在其中将一个字符串绑定到一个变量。 有没有一种简单的方法可以将变量递增 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']
试试这个:
$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++});
只需创建一个键数组,然后您可以使用 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);