How to read txt file and mount php array?

Asked

Viewed 569 times

-1

I wonder if there is any way to read a file . txt and mount a PHP array with each line of the file . txt to then mount JSON if anyone can help me thank you!

I want the exhibition to stay that way:

{
"empregados": [
    {
        "nome": "Jason Jones",
        "idade": 38,
        "sexo": "M"
    },
    {
        "nome": "Ada Pascalina",
        "idade": 35,
        "sexo": "F"
    },
    {
        "nome": "Delphino da Silva",
        "idade": 26,
        "sexo": "M"
    }
]

}

This is my code:

<?php $dadox = array();
$dado = fopen("new_2.txt", "r");



while (!feof ($dado)) {
//se extraio uma linha do arquivo e nao eh false
if ($linha = fgets($dado)){
$dadox[] = $linha;
}

$cpf = substr("$linha", 0, 11 );
$nome = substr("$linha", 11, 32 );
$end = substr("$linha", 43, 28 );
$com = substr("$linha", 75 , 8 );
$cidade = substr("$linha", 87 , 13 );
$uf = substr("$linha", 103 , 2 );


//fclose($arquivo);
header('Content-Type: application/json');


 $funcionario =
     array(
       'CPF'=>"$cpf",
        'Nome'=>"$nome",
        'Endereco'=>"$end",
         'Complemento'=>"$com",
         'Cidade'=>"$cidade",
          'UF'=>"$uf"

     );
     $dados = array(
       $funcionario
     );

    $dados_funcionario = array('funcionario' => $dados);

    $json_str = json_encode($dados_funcionario,JSON_PRETTY_PRINT);



    echo "$json_str";
//var_dump($funcionario);
} ?>
  • 1

    Yes, many ways, but all depend on the format of your file. Could [Edit] your question and add an example and describe what is the desired output?

  • I think this question may help you: https://answall.com/questions/189409/como-transform-esse-texto-em-um-array-com-nome-e-descri%C3%A7%C3%A3o

  • How it is delimited?

1 answer

0


As has been mentioned, there are N ways to resolve this issue, below I will present a supposing that the data are delimited according to your example.

This would be your new_2.txt

12345678912primeiro nome do cliente        endereço 1                  comp 1  cidade 1     SP
12345678912segundo nome do cliente         endereço 2                  comp 2  cidade 2     MG

It follows as would a possible solution

<?php

$dadox['empregados'] = array();
$lines = file('new_2.txt');
foreach ($lines as $line) {
    if (preg_match('#^(?<cpf>[0-9]{11})(?<nome>[^\n]{32})(?<end>[^\n]{28})(?<com>[^\n]{8})(?<cidade>[^\n]{13})(?<uf>[^\n]{2})#iu', trim($line), $arr)) {
        $dadox['empregados'][] = array(
            'cpf' => $arr['cpf'],
            'nome' => $arr['nome'],
            'end' => $arr['end'],
            'com' => $arr['com'],
            'cidade' => $arr['cidade'],
            'uf' => $arr['uf']
        );
    }
}

echo '<pre>';
print_r($dadox);

I won’t go into detail about regex, as you can see an example of it running here https://regex101.com/r/5TyRzk/1 and completely detailed.

  • Vlw bro! Exactly what I wanted.

Browser other questions tagged

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