Writing and retrieving an array

A variable $totalis 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 the created file later so that I can work with the array inside it? How:

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

Thank.

+3
source share
1 answer

You need to serialize it using serializelike this:

$serialize_array = serialize($array);

Now you can save $serialize_arrayto your file. To read it and convert it to an array again, use a function unserialize.

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));
+7
source

Source: https://habr.com/ru/post/1759093/


All Articles