?
Solved

PHP how to serialize

Posted on 2005-04-21
5
Medium Priority
?
2,003 Views
Last Modified: 2007-12-19
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());
?>
0
Comment
Question by:rblampain
5 Comments
 
LVL 14

Assisted Solution

by:cracky
cracky earned 400 total points
ID: 13840596
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
 
LVL 14

Expert Comment

by:cracky
ID: 13840617
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
 
LVL 8

Accepted Solution

by:
inq123 earned 1200 total points
ID: 13842590
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
 
LVL 3

Assisted Solution

by:designbai
designbai earned 400 total points
ID: 13848786
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
 

Author Comment

by:rblampain
ID: 13864536
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question