你做你递增for
循环外,并且只能访问[0]
th元素,所以在其他地方没有任何变化。
这也许应该是这个样子:
$filename = 'a.txt';
$lines = file($filename);
// $k = key, $v = value
foreach ($lines as $k=>$v) {
$exploded = explode("|", $v);
// Does this match the site name you're trying to increment?
if ($exploded[0] == "some_name_up_to_you") {
$exploded[1]++;
// To make changes to the source array,
// it must be referenced using the key.
// (If you just change $v, the source won't be updated.)
$lines[$k] = implode("|", $exploded);
}
}
// Write.
file_put_contents($filename, $lines);
你或许应该使用这个数据库,虽然。看看PDO和MYSQL,你会走向迷人的路。
编辑
要你在你的评论中提到的,你可以设置一个布尔标志,并触发它,你走过的阵列。这可以保证一个break
也一样,如果你只是在寻找一两件事:它存储在后您使用的是foreach
循环
...
$found = false;
foreach ($lines as $k=>$v) {
$exploded = explode("|", $v);
if ($exploded[0] == "some_name_up_to_you") {
$found = true;
$exploded[1]++;
$lines[$k] = implode("|", $exploded);
break; // ???
}
}
if (!$found) {
$lines[] = "THE_NEW_SITE|1";
}
...
来源
2012-11-28 05:42:17
Ben
“.txt”文件中有多少行......? – NazKazi
发布你的'txt'文件的一些示例内容 –
一个数据库比这个文本文件要容易得多 – 2012-11-28 05:49:38