This is a stupid example, but shows exactly what is my problem. In some situations the array is sucessufully modified and anothers it is not, why? Values are given to foreach by value? And the output is also screwed, some lines seems to have '\r\n' others do not.
$arr = file('text.txt');
echo '<pre>';
foreach( $arr as $x => $line){
if( $x % 3){ unset( $arr[$x]); } // this works
else{ $arr[$x+1] += 1;} // this don't
echo "[$x] => ${arr[$x+1]}";
}
print_r( $arr);
text.txt:
0
1
2
3
4
5
6
çıktı:
[0] => 2[1] => 2
[2] => 3
[3] => 5[4] => 5
[5] => 6[6] => 1Array
(
[0] => 0
[3] => 3
[6] => 6
[7] => 1
)
EDIT:
Örnek gerçekten sadece beklenmedik bir şey oldu ki göstermek işe yaramaz, bu yüzden, burada yapmam gereken ne yakın bir şeydir, bir şey başarmak vermedi:
<?php
$arr = file('text.txt');
echo '<pre>';
foreach( $arr as $x => $line){
if( preg_match("/word$/", $line)){
$line = preg_replace( "/word$/", '', $line);
$arr[$x+1] = 'word ' . $arr[$x+1];
}
}
print_r( $arr);
text.txt:
test0
test1word
test2
dizide beklenen değerler:
[0] => test0
[1] => test1
[2] => word test2