I am trying to read txt file, find $id and increment it's value $id=>value
. But the problem is that when I want to save changes the first new line is ignored so
10=>26
30=>11
33=>22
became this after I run the script
10=>2730=>11
33=>22
This
10=>26
30=>11
33=>22
Should result in this
10=>27
30=>11
33=>22
Here is the code. I am using $id = 10
.
$tmp= "";
$file = file_get_contents(dirname(__FILE__) . "/requests.txt", "r");
$file = explode("\n", $file);
foreach ($file as $product){
$data = explode("=>", $product);
if($data[0] == $id) {
$data[1] += 1;
$product = $data[0]."=>".$data[1];
$tmp = $tmp.$product."\n";
continue 1;
}
$tmp = $tmp.$product."\n";
}
echo $tmp;
file_put_contents(dirname(__FILE__) . "/abc.txt", $tmp);