PHP how to serialize

I will only learn if I have to.
A few experts have advised to use "serialize" and "unserialize" in a previous question. After a bit of investigation I find I don't know how to do it.

The problem I have to solve is the creation of a multi-dimension array that has to be saved as a flat file and then retrieved and reconstructed from the flat file on demand.

I would appreciate if anyone could give some example of coding this.
Thank you for your help.

Here is an example of the array:
<?
$layout["Copyright"]=array("Liability" => array(),"Warranty" => array(),"Disclaimer" => array(),"Trademarks" => array());
$layout["About_the_Author"]=array("Identity" => array(),"Address" => array(),"Qualifications" => array());
$layout["Credits"]=array();
$layout["Preface"]=array();
$layout["Acknowledgments"]=array();
$layout["Contents"]=array("Tome 1" => array("Part I" => array(),"Part II" => array(),"Part III" => array(),"Part IV" => array(),"Part V" => array()));
$layout["Appendices"]=array("Appendix A" => array(),"Appendix B" => array(),"Appendix C" => array());
$layout["Index"]=array();

$layout["Contents"]["Tome 1"]["Part I"]=array("Chapter 1" => array(),"Chapter 2" => array(),"Chapter 3"=> array(),"Chapter 4"=> array());
$layout["Contents"]["Tome 1"]["Part II"]=array("Chapter 5"=> array(),"Chapter 6"=> array(),"Chapter 7"=> array(),"Chapter 8"=> array(),"Chapter 9"=> array());
$layout["Contents"]["Tome 1"]["Part III"]=array("Chapter 10"=> array(),"Chapter 11"=> array(),"Chapter 12"=> array(),"Chapter 13"=> array(),"Chapter 14"=> array());
$layout["Contents"]["Tome 1"]["Part IV"]=array("Chapter 15"=> array(),"Chapter 16"=> array(),"Chapter 17"=> array(),"Chapter 18"=> array(),"Chapter 19"=> array(),"Chapter 20"=> array(),"Chapter 21"=> array(),"Chapter 22"=> array(),"Chapter 23"=> array(),"Chapter 24"=> array(),"Chapter 25"=> array(),"Chapter 26"=> array());
$layout["Contents"]["Tome 1"]["Part V"]=array("Chapter 27"=> array(),"Chapter 28"=> array());
?>
rblampainAsked:
Who is Participating?
 
inq123Connect With a Mentor Commented:
Hi rblampain,

file_get_contents() requires php to be at least 4.3, if your php's not high enough, use join('', file($filename)) instead.

BTW, if you need some code, here's some that works well based on your code:

<?
$layout["Copyright"]=array("Liability" => array(),"Warranty" => array(),"Disclaimer" => array(),"Trademarks" => array());
$layout["About_the_Author"]=array("Identity" => array(),"Address" => array(),"Qualifications" => array());
$layout["Credits"]=array();
$layout["Preface"]=array();
$layout["Acknowledgments"]=array();
$layout["Contents"]=array("Tome 1" => array("Part I" => array(),"Part II" => array(),"Part III" => array(),"Part IV" => array(),"Part V" => array()));
$layout["Appendices"]=array("Appendix A" => array(),"Appendix B" => array(),"Appendix C" => array());
$layout["Index"]=array();

$layout["Contents"]["Tome 1"]["Part I"]=array("Chapter 1" => array(),"Chapter 2" => array(),"Chapter 3"=> array(),"Chapter 4"=> array());
$layout["Contents"]["Tome 1"]["Part II"]=array("Chapter 5"=> array(),"Chapter 6"=> array(),"Chapter 7"=> array(),"Chapter 8"=> array(),"Chapter 9"=> array());
$layout["Contents"]["Tome 1"]["Part III"]=array("Chapter 10"=> array(),"Chapter 11"=> array(),"Chapter 12"=> array(),"Chapter 13"=> array(),"Chapter 14"=> array());
$layout["Contents"]["Tome 1"]["Part IV"]=array("Chapter 15"=> array(),"Chapter 16"=> array(),"Chapter 17"=> array(),"Chapter 18"=> array(),"Chapter 19"=> array(),"Chapter 20"=> array(),"Chapter 21"=> array(),"Chapter 22"=> array(),"Chapter 23"=> array(),"Chapter 24"=> array(),"Chapter 25"=> array(),"Chapter 26"=> array());
$layout["Contents"]["Tome 1"]["Part V"]=array("Chapter 27"=> array(),"Chapter 28"=> array());

print_r($layout); # print out the data structure before serialization
$fh = fopen("/tmp/flatfile", "w");
fwrite($fh, serialize($layout));
fclose($fh);

# now let's read it out
print "<br>\nNow after deserialization:<br>\n";
$newlayout = unserialize(join('', file("/tmp/flatfile")));
print_r($newlayout); # print out the data structure after deserialization, you'll see it's the same structure
?>

BTW, don't forget to make sure your web server has permission to write to the flatfile you want to create.

Cheers!
0
 
crackyConnect With a Mentor Commented:
The fwrite section of the PHP manual has a perfect example:

http://www.php.net/manual/en/function.fwrite.php

Just change:
$somecontent = "Add this to the file\n";

to:
$somecontent = serialize($layout);

at the end of your code.

Then, to read it back (as long as you know the filename and path):

http://au3.php.net/manual/en/function.fread.php

I would post the code for you, but I don't want to plagiarise the examples and there's no point in me re-writing it, since it would look almost exactly the same. The functions are fairly straightforward. Remember that your filesystem must be writable by the user your webserver runs as. apache or www or another user, depending on your config.
0
 
crackyCommented:
Oh, don't forget to use unserialize on the data read from your file:

I just realised I forgot you can also use file_get_contents().

So, for example, you could use this in the page where you need to retrieve the array:

<?php
$layout = unserialize(file_get_contents('myfile.txt'));
?>

$layout will now be exactly as you left it the last time you saved the array.
0
 
designbaiConnect With a Mentor Commented:
Here is some tips when serialize at Action.

If you are storing serialized data in a database, you need to serialize() the data, *then* addslashes(), then stick it in the DB.
At the time of fetching it back from the db, stripslashes() (if you don't automatically do it), then unserialize().

If you are passing serialized data between pages in hidden form fields, you need to serialize() the data, then *urlencode()* it, then put it in the hidden field.
When you get to the next page, urldecode() it, then unserialize().

If you are passing serialized data in a query string, you don't need to urldecode() it on the following page.  Just unserialize().

hope this helps
0
 
rblampainAuthor Commented:
Thanks to all. Code examples that suit my application are very important to me,  they will often save me at least the time to find and adapt the examples in the docs or manuals.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.