Create JSON in a txt file

Asked

Viewed 669 times

0

How do I turn a.txt file into json ?

$linhas = fopen ("sections.txt", "r");      


        //percorre                                 
       while (!feof ($linhas)){

         $ponteiro = fgets($linhas, 1096);
            $valores = preg_split("[/]",$ponteiro);

           $paraTxt = $valores[0];

           //não pega o ; inicial do arquivo
           $paraTxt = substr ($valores[0],1);// aqui
           // echo '<br>'. $paraTxt;


           //novo array php
           $arrSection[] = array($paraTxt);                   
             $paraJson = json_encode($arrSection);          

         }
        // $paraJson = json_encode($arrSection);

         var_dump($paraJson); 
         //fclose ($linhas); */



do jeito que fiz retornando false.

php:238:Boolean false reader

Any hint of turning this file,where it already reads and traverses in txt format, and when I use Function to turn into json returns me false That at the end I want to call this array in javascript,be listed in typehead.

  • When you echo in $paraTxt something comes up? the json_encode() should be out of the while. It can simplify $arrSection[] = array($paraTxt); for $arrSection[] = $paraTxt; then the generated array has only one dimension.

  • Yes ,the content of the txt file appears, ie it read... before json_encode , when I use the function returns me false

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.