J
J
Jony13372017-01-04 18:41:19
PHP
Jony1337, 2017-01-04 18:41:19

What is the correct way to perform this task in php?

I can’t solve this problem, the fact is that there are 100 .txt files in one folder, you need to open them one by one, decode the array from where and perform further actions with it, then save it all to another file.
That's how I did it

$nm = 'tm-contents/e0.txt';
$homepage = file_get_contents($nm);
$ins = json_decode ( $homepage , true);
for ($i=1; $i <= 500; $i++) {
 
//Тут код который не имеет отношения к вопросу 

//Сохраняем после редактирования 
  $json= json_encode($rs);
  $nextNameFisier = 'rs/alx'.$i.'.txt';
$f = fopen($nextNameFisier, "w");
fwrite($f, $json); 
fclose($f);
}

I thought to make two FORs, but the fact is that the first one should iterate over the names from e0.txt to e100.txt and the second number of the array element, that is, in the first file e0.txt there is an array with an index from 1 to 500, in the second it goes from 500 to 1000 and so on. Who has an idea?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy Orlov, 2017-01-04
@Jony1337

you need to use foreach like this

foreach (glob('tm-contents/*.txt') as $filename) {
   $fileId = preg_replace('~\D~','',$filename); // удаляем все кроме цифр
   $data = json_decode(file_get_contents('tm-contents/'.$filename));
   foreach ($data as $key=>$arr) {
      // секретные действия с $data
   }
   file_put_contents ('rs/alx'.$fileId.'.txt', json_encode($data));
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question