W
W
weranda2018-02-07 22:19:51
PHP
weranda, 2018-02-07 22:19:51

How to convert file data to array in PHP?

There is a text file with the following content:

Имя * значение
Имя * значение
...

As far as I understood, the command $file = file('list-of-districts.txt'); reads and forms an array like:
[0] => Имя * значение,
[1] => Имя * значение,
...

But how to quickly, correctly and least costly form an array of this type from a file?
'Имя' => 'Значение',
'Имя' => 'Значение',
...

PS
Or maybe a less resource-intensive and faster option is JSON?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SteepZero, 2018-02-07
@SteepZero

fgetcsv with ' * ' delimiter Looping through rows
For
each row you get an array with two elements
And you use the first element as a key, the second as a value for a new array

O
Oleg, 2018-02-07
@402d

list-of-districts.txt how often does it change?
if mtime(list-of-districts.txt) > mtime(list-of-districts.json) then
how can you make a json version of the file
if you want
to display the necessary response headers for json
output quickly via fpassthru($fp);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question