Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a php array saved in a file?

Tags:

php

file-io

i have some php arrays saved in a .log file

i want to read them into a php array such as

array[0] = 1st array in the .log file array1 = 2nd array in the .log file

the solution here does not work for me

it gives no such file or directory error but when i do include_once('file.log') the content in the file is displayed as the output ( i dont know why ) please help

like image 286
shaheer Avatar asked Feb 14 '11 20:02

shaheer


People also ask

How do I get an array from a file?

In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method.

How do I view an array in PHP?

To display array structure and values in PHP, we can use two functions. We can use var_dump() or print_r() to display the values of an array in human-readable format or to see the output value of the program array.

How do I save an array text file in PHP?

How do I save an array text file in PHP? Use the print_r() and file_put_contents() Functions to Write the Array to the File in PHP. Use fopen() , print_r() and fwrite() Functions to Write the Array to the File in PHP. Use fopen() , var_export() and fwrite() Functions to Write the Array to the File.


1 Answers

You could serialize the array before writing it as text to a file. Then, you can read the data back out of the file, unserialize will turn it back into an array.

http://php.net/manual/en/function.serialize.php

EDIT Describing the process of using serialize/unserialize:

So you have an array:

$arr = array(
  'one'=>array(
      'subdata1',
      'subdata2'
  ),
  'two'='12345'
);

When I call serialize on that array, I get a string:

$string = serialize($arr);
echo $string;

OUTPUT: a:2:{s:3:"one";a:2:{i:0;s:8:"subdata1";i:1;s:8:"subdata2";}s:3:"two";s:5:"12345";}

So I want to write that data into a file:

$fn= "serialtest.txt";
$fh = fopen($fn, 'w');
fwrite($fh, $string);
fclose($fh);

Later, I want to use that array. So, I'll read the file, then unserialize:

$str = file_get_contents('serialtest.txt');
$arr = unserialize($str);
print_r($arr);

OUTPUT: Array ( [one] => Array ( [0] => subdata1 [1] => subdata2 ) [two] => 12345 ) 

Hope that helps!

EDIT 2 Nesting demo

To store more arrays into this file over time, you have to create a parent array. This array is a container for all the data, so when you want to add another array, you have to unpack the parent, and add your new data to that, then repack the whole thing.

First, get your container set up:

// Do this the first time, just to create the parent container
$parent = array();
$string = serialize($arr);
$fn= "logdata.log";
$fh = fopen($fn, 'w');
fwrite($fh, $string);
fclose($fh);

Now, from there forward, when you want to add a new array, first you have to get the whole package out and unserialize it:

// get out the parent container
$parent = unserialize(file_get_contents('logdata.log'));

// now add your new data
$parent[] = array(
  'this'=>'is',
  'a'=>'new',
  'array'=>'for',
  'the'=>'log'
);

// now pack it up again
$fh = fopen('logdata.log', 'w');
fwrite($fh, serialize($parent));
fclose($fh);
like image 82
Chris Baker Avatar answered Sep 22 '22 10:09

Chris Baker