Skip to content
Advertisement

Best way to write arrays to a file? [closed]

I want to avoid writing to DB and use constants/array for lang files etc.

i.e:

$lang = array (
  'hello' => 'hello world!'
);

and be able to edit it from the back office. (then instead of fetching it from the poor db, i would just use $lang[‘hello’]..).

what are you suggesting for the best and efficient way to pull it of?

Advertisement

Answer

Definitely JSON

To save it :

file_put_contents("my_array.json", json_encode($array));

To get it back :

$array = json_decode(file_get_contents("my_array.json"));

As simple as that !

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement