Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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?

like image 844
WEBProject Avatar asked Sep 21 '12 12:09

WEBProject


People also ask

How do I write an array to a file?

We can create an array and use print_r() function to return the array and use the fwrite() function to write the array to the file. The print_r() function takes the array to be printed and the boolean value as the parameters. Use the fopen() function to create a file file. txt with w as the second parameter.

How do you write the contents of an array to a file in C?

Since the size of the data is fixed, one simple way of writing this entire array into a file is using the binary writing mode: FILE *f = fopen("client. data", "wb"); fwrite(clientdata, sizeof(char), sizeof(clientdata), f); fclose(f);

How do I save an array text file in Python?

Let us see how to save a numpy array to a text file. Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file using the write() function. Finally closing the file using close() function.


1 Answers

the most efficient way i found looks like this:

build up your array in php somehow and export it into a file using var_export()

file_put_contents( '/some/file/data.php', '<?php return '.var_export( $data_array, true ).";\n" );

then later, wherever you need this data pull it like this

$data = include '/some/file/data.php';
like image 61
Jan Prieser Avatar answered Oct 06 '22 01:10

Jan Prieser