0

Hello i have a text file in which i record some arrays with output of print_r as below

$filename = 'record.txt';
$fp = fopen($filename, 'w');
$arr = [['MODEL' => 2011, 'BRAND' => 'FIAT'], ['MODEL' => 2017, 'BRAND' => 'MERCEDES']];
fwrite($fp, print_r($arr,true));
fclose($fp);

the output of this file is like this:

Array
(
    [0] => Array
        (
            [MODEL] => 2011
            [BRAND] => FIAT
        )

    [1] => Array
        (
            [MODEL] => 2017
            [BRAND] => MERCEDES
        )

)

now i want to read it back and use it as array. Is there any simple way to parse this string.

  • 3
    It's possible. But hardly easy nor advisable. Use JSON in the future. – mario Oct 15 '17 at 13:36
  • 1
    https://stackoverflow.com/questions/2662268/how-do-i-store-an-array-in-a-file-to-access-as-an-array-later-with-php – Madhawa Priyashantha Oct 15 '17 at 13:38
  • If you have to write to file like this for subsequent reload, use [var_export()](http://www.php.net/manual/en/function.var-export.php) rather than print_r() – Mark Baker Oct 15 '17 at 15:04

0 Answers0