Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a array that I am writing to a file using var_export(). I reload the array every time the script starts. However, whenever I try to reference a variable inside the array it returns 'a', I can do a print_r() and see the array just fine, I just can not access the variable I want. Here is the saved output:

array (
  'timestamp' => '1283882964',
  'files_submitted' => 2943,
  'errors' => array (
                     '/home/benk/bull_copy/bull_mnt//WebFS/Projects/Inactive/2002_Product_Testing/tests/Functional Test  Cases 2001122301.doc' => array (
                                                                                                                                                         'STATUS' => 400,
                                                                                                                                                  ),
                     '/home/benk/bull_copy/bull_mnt//WebFS/Projects/Inactive/2002_Product_Testing/tests/Functional Test  Cases for atrust 20020905.doc' => array (
                                                                                                                                                                  'STATUS' => 400,
                                                                                                                                                            ),
              )
)

Here is the code I use to save:

function add_log_entry($filename,$return_arr) {
        //$timestamp = strval(mktime());
        $return_arr['timestamp'] = mktime();
        $return_str = var_export($return_arr,true);
        return file_put_contents($filename, $return_str);
}

Here is the code I use to recall the array:

function get_log_entry($filename) {

        $var_str = file_get_contents($filename);
        eval("\$return_var = \$var_str;");
        die($return_var['timestamp']);
        return $return_var;
}

You can see I put the die() in the recall code and this is where the 'a' is coming from.

Thanks to whom ever responds.

Ben

share|improve this question
1  
Why do you use an array you cannot refer to? Ever considered to use a less complex (for you) structure? –  Your Common Sense Sep 7 '10 at 20:38

1 Answer

up vote 2 down vote accepted

use the php functions serialize and unserialize, no need to write your own hacks using var_export and eval (apart from the security implications)

example code:

 file_put_contents($filename, serialize($array));
 $array = unserialize(file_get_contents($filename));

Using serialize/unserialize might impose a security risk. To serialize simple arrays/data structures it's better to use the json_encode function:

 file_put_contents($filename, json_encode($array));
 $array = json_decode(file_get_contents($filename), TRUE);
share|improve this answer
 
This answer is old and outdated. unserialize might impose security risks. If you just need to serialize an array, better use json_decode –  knittl Jul 24 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.