tags:

views:

33

answers:

1

Variable $total is an array().

print_r($total) gives:

Array (
    [01] => Array ( [title] => text [date] => date )
    [02] => Array ( [title] => text [date] => date )
    [03] => Array ( [title] => text [date] => date )
)

How to write this array to file.txt?

And how to call created file later, so I can work with array inside it? Like:

$extracred_array = file.txt;
echo $extracred_array[1][title];

Thanks.

+5  A: 

You need to serialize it with serialize function like this:

$serialize_array = serialize($array);

Now you can save the $serialize_array in your file. To read it back and convert to array again, use the unserialize function.

Update:

// write array data to file
file_put_contents('file.txt', serialize($your_array));    

To read the file back:

// read array back from file
$contents = file_get_contents('file.txt');

// show the array
print_r(unserialize($contents));
Sarfraz
serialize() just converts array to object?
Happy
please tell how to >>Now you can save the $serialzed_array in your file. To read it back and convert to array again, use the unserialze function.
Happy
@Ignatz: It generates a storable representation of a value, pretty ideal in your situation or when saving arrays into database.
Sarfraz
@Sarfraz, thanks. What about the second comment.
Happy
@Ignatz: See my update please.
Sarfraz