12

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

Community
  • 1
  • 1
shaheer
  • 376
  • 2
  • 6
  • 16
  • Please show an example of the format the arrays are stored in the file, so we know if they can be read back into PHP. – Gordon Feb 14 '11 at 21:15
  • 1
    possible duplicate of [Reading and Writing Configuration Files](http://stackoverflow.com/questions/2237291/reading-and-writing-configuration-files) – ircmaxell Feb 14 '11 at 21:22
  • @gordon, you have used print_r ? thats the format, actually the data is private paypal related data, so i can't post. – shaheer Feb 14 '11 at 21:33
  • @shaheer can you change the format how the files get stored? Can you use any of the formats suggested in the linked duplicate instead? Then you don't have any issues. – Gordon Feb 14 '11 at 21:36
  • i can't because it will require heavy coding and will slow down my scripts i am using chris's solution and this is working fine – shaheer Feb 14 '11 at 22:13
  • @shaheer no offense, but if you can serialize the arrays before writing them to script you can also use a different format. Plus, serializing isn't exactly fast, whereas the pure array variant shown in the linked answer does not have that overhead. It also doesnt require "heavy coding". But use what you will. – Gordon Feb 14 '11 at 22:53

4 Answers4

19

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);
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
  • how would i read it even after that? currently var_dump( unserialize ($str ) ); gives bool(false) – shaheer Feb 14 '11 at 21:12
  • Oh, I think I am misunderstanding your question. It occurs to me now that you have EXISTING arrays in a text file, and you are just trying to read them as a one-off type of thing. Right? – Chris Baker Feb 14 '11 at 21:20
  • 3
    Pedantic note: **should** in the answer should become **could** since there are lots of other methods (some which *may* be better depending on usecase)... – ircmaxell Feb 14 '11 at 21:22
  • @chris yes you are right though i am now using serialize to write the data, and your solution works fine except for one situation. it only reads one single array, how can i make so that all the arrays are read? – shaheer Feb 14 '11 at 21:34
  • If you are going to pack multiple arrays into your file, then you would use array nesting. I'll update my answer to show ya. :) – Chris Baker Feb 14 '11 at 21:41
2

It really takes 2 lines of code ($ar is the array and $filename is the path+filename to where you want to save):

To save the array:

file_put_contents($filename, serialize($ar));

to read the array back:

$ar=unserialize(file_get_contents($filename));

JG Estiot
  • 969
  • 1
  • 10
  • 8
1

include_once displays output, so that's not unexpected. Can you please provide the file format?

You can use file_get_contents('file.log') to get the contents of the file, then easily put it into an array from there (eg using preg_split).

Highly Irregular
  • 38,000
  • 12
  • 52
  • 70
  • Should this be a comment? Oh, right, you need points to comment, don't you? My bad! – jocap Feb 14 '11 at 21:06
  • file_get_contents('file.log') gives file or directory not found error, i said that in the description – shaheer Feb 14 '11 at 21:14
  • 1
    `include_once` displays output? Interesting. Please file a bug report with http://php.net/manual/en/function.include-once.php and inform them about it, because the docs suggest different. – Gordon Feb 14 '11 at 21:16
  • the file isn't a php file, so perhaps it is fine? – shaheer Feb 14 '11 at 21:28
  • Thanks for the sarcasm Gordon, however the include function outputs everything that's not between code tags, which would generally be the entire file when it's a log file not a php file. Make sense? – Highly Irregular Feb 25 '11 at 02:52
1

you need to "return" the array , open the log file.log and at the top add return

Mr Coder
  • 8,169
  • 5
  • 45
  • 74