12

I've saved a response from an outside server to a text file, so I don't need to keep running connection requests. Instead, perhaps I can use the text file for my manipulation purposes, until I'm read for re-connecting again. (also, my connection requests are limited to this outside server)

Here is what I've saved to a text file:

records.txt

Array
(
    [0] => stdClass Object
        (
            [id] => 552
            [date_created] => 2012-02-23 10:30:56
            [date_modified] => 2012-03-09 18:55:26
            [date_deleted] => 2012-03-09 18:55:26
            [first_name] => Test
            [middle_name] => 
            [last_name] => Test
            [home_phone] => (123) 123-1234
            [email] => [email protected]
        )
     [1] => stdClass Object
        (
            [id] => 553
            [date_created] => 2012-02-23 10:30:56
            [date_modified] => 2012-03-09 18:55:26
            [date_deleted] => 2012-03-09 18:55:26
            [first_name] => Test
            [middle_name] => 
            [last_name] => Test
            [home_phone] => (325) 558-1234
            [email] => [email protected]
        )
)

There's actually more in the Array, but I'm sure 2 are fine.

Since this is a text file, and I want to pretend this is the actual outside server (sending me the same info), how do I make it a real array again?

I know I need to open the file first:

<?php
$fp = fopen('records.txt', "r"); // open the file
$theData = fread($fh, filesize('records.txt'));
fclose($fh);
echo $theData;  
?>

So far $theData is a string value. Is there a way to convert it back to the Array it originally came in as?

0

4 Answers 4

32

Better serialize and save to file, then unserialize back to array.

// serialize your input array (say $array)
$serializedData = serialize($array);

// save serialized data in a text file
file_put_contents('your_file_name.txt', $serializedData);

// at a later point, you can convert it back to array like:
$recoveredData = file_get_contents('your_file_name.txt');

// unserializing to get actual array
$recoveredArray = unserialize($recoveredData);

// you can print your array like
print_r($recoveredArray);
Sign up to request clarification or add additional context in comments.

1 Comment

hi, I assume that $array have contain all of the data, what if the data comes one by one, so i will have bunch of serialized data, when i unserialize the textfile... it just unserialized the last of serialized data.. can i unserialized the data and set it as one array?
4

You shouldn't have saved it in print_r format then.

Use either:

That makes it simple to decode the file back into an array.

Albeit there is a print_r decoder. But that should be the last resort, only if you cannot affect the input data (which you can!).

7 Comments

Are you sure var_export'ed data can be converted back to array?!! FYI, php.net manual says another fact: var_export() does not handle circular references as it would be close to impossible to generate parsable PHP code for that. If you want to do something with the full representation of an array or object, use serialize().
@rajukoyilandy: Neither can serialize or json_encode. Circular references are an occassion in object structures, but not in plain arrays as OP has em. var_export files are simple php snippets to be commonly include()d again. One should use that carefully, but it's invaluable for configuration files.
var_export is same as print_r or var_damp
@mario var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.
I think json_encode should be on the top of the list because that will allow OP to read the data from both PHP/non-PHP platforms.
|
3

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.

Comments

2

JSON Version

$json_data = json_encode($the_array);
file_put_contents("records.txt", $json_data);

// Recovering
$the_data = file_get_contents("records.txt");
$the_array = json_decode($the_data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.