/    Sign up×
Community /Pin to ProfileBookmark

Hi,
Can any one tell me about php serialize and unserialize function with example? How these function work with forms array fields? If i want to post multi-dementional array data how i post these values?…

Waiting for any good response…………

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@Jarrod1937Nov 01.2010 — No example is really needed for the serialize and unserialize functions. Essentially serialize() takes a variable with multiple dimensions like an array, and outputs a flat string. This allows you to store the data or to transport it easier. You can simply run the output through unserlaize and it will restore the array from the string. Checkout the official docs (they also give examples):

http://php.net/manual/en/function.serialize.php
Copy linkTweet thisAlerts:
@bc080400222authorNov 01.2010 — hi jarrod,

Thank you so much for your response... Can you please take a loot on my form i want to post form values using serialize and unserialize function or implode or explode function. But i prefer wich function that full fill my requirements. My html form code is below:

<form id="form1" name="form1" method="post" action="">

<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">

<?php for($i=0; $i<2; $i++){ ?>

<tr>

<td width="19%" height="30">&nbsp;</td>

<td width="16%" align="left">First Name : </td>

<td width="13%" align="left"><input type="text" name="first[]" /></td>

<td width="52%">&nbsp;</td>

</tr>

<tr>

<td height="30">&nbsp;</td>

<td align="left">Last Name: </td>

<td align="left"><input type="text" name="last[]" /></td>

<td>&nbsp;</td>

</tr>

<tr>

<td height="30">&nbsp;</td>

<td align="left">Age : </td>

<td align="left"><input type="text" name="age[]" /></td>

<td>&nbsp;</td>

</tr>

<tr>

<td height="30">&nbsp;</td>

<td align="left">&nbsp;</td>

<td align="left">&nbsp;</td>

<td>&nbsp;</td>

</tr>

<?php } ?>

<tr>

<td height="30">&nbsp;</td>

<td align="left">&nbsp;</td>

<td align="left"><input type="submit" name="Submit" value=" Submit " /></td>

<td>&nbsp;</td>

</tr>

</table>

and my php code is:



<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td height="30" colspan="3" align="center" bordercolor="#999999" bgcolor="#CCCCCC"><strong>Serialized</strong></td>

<td height="30" align="center" bordercolor="#999999">&nbsp;</td>

<td height="30" colspan="3" align="center" bordercolor="#999999" bgcolor="#CCCCCC"><strong>Unserialized</strong></td>

</tr>

<tr>

<td width="19%" align="left"><strong>First Name</strong></td>

<td width="19%" align="left"><strong>Last Name</strong></td>

<td width="15%" align="left"><strong>Age</strong></td>

<td width="8%" align="left">&nbsp;</td>

<td width="13%" align="center"><strong>First</strong></td>

<td width="15%" align="center"><strong>Last</strong></td>

<td width="11%" align="center"><strong>Age</strong></td>

</tr>

<?php

$first = $_REQUEST["first"];

$last = $_
REQUEST["last"];

$age = $_REQUEST["age"];

$ser_first2 = serialize($first);

$ser_last2 = serialize($last);

$ser_age2 = serialize($age);

$unserial_first = unserialize($ser_first2);

$unserial_last = unserialize($ser_first2);

$unserial_age = unserialize($ser_first2);

for($k=0; $k<count($unserial_first); $k++){

?>

<tr>

<td align="left"><?=$ser_first2?></td>

<td align="left"><?=$ser_last2?></td>

<td align="left"><?=$ser_age2?></td>

<td align="left">&nbsp;</td>

<td align="center"><?php echo $unserial_first[$k]; ?></td>

<td align="center"><?php echo $unserial_last[$k]; ?></td>

<td align="center"><?php echo $unserial_age[$k]; ?></td>

</tr>

<? } ?>

</table>


Jarrod the above code is work fine only with first variable ( $unserial_first[$k] ) and show its value only others values is not showing. Can you please take a look what is the error in this code? and how i fix it?

And please also tell me what is the best method to post multidemensional array values. I always have an to post array values. Please assist me regarding this problem i will be apprietiate it. Thanks for assist.

waiting for your good response..........
Copy linkTweet thisAlerts:
@Jarrod1937Nov 01.2010 — Start with some basic debugging, print out the result for $_POST['first'] using:

[code=php]print_r($_POST['first']);[/code]

at the top of your page before any processing done is on it. This will tell you if your form is setup properly and php is converting the fields into an array.

Though i am unclear as to what your goal is with serialize, what are you trying to accomplish by using it?
Copy linkTweet thisAlerts:
@bc080400222authorNov 01.2010 — Thanks jarrod for your quick response.

As you said i tried. I entered waqar and vicky values in { First Name1 and First Name2 }. And i found as a printa result below.

<?php print_r($_POST['first']); ?>

Result:

Array ( [0] => waqar [1] => vicky )
Copy linkTweet thisAlerts:
@bc080400222authorNov 01.2010 — i am working on big project and i want to insert data into database but i am using this serialize function because i want to insert values same like above three fields into only one database colum. I have read about serialize and unserialize functions that helps me regarding this matter. I also have read about implode and explode functions that works same like serialize but storing format is different. Jarrod can you please assist me regarding this matter so that real implement it on my project.

And please assist me about what is the best method to post arrays values?. Thanks for your reponse.
Copy linkTweet thisAlerts:
@Jarrod1937Nov 02.2010 — Ok, so from what i now understand, you're trying to take an array generated by your html form, store it in a database as a string using serialize, then unserialize it at a later time and get the array back. Correct? However, you say you're having issues where it doesn't restore the entire array? Correct?

Well, so for it looks like it is setup correctly, but you should just do some basic debugging. Echo out the results of the serialized arrays, make sure all of the data is there, then after you unserialize them, do another print_r(); just to make sure it restored correctly. Print_r() is better to use than your for loop for debugging (introduces less code).
Copy linkTweet thisAlerts:
@criterion9Nov 02.2010 — This sounds like a good candidate for database normalization rather than data mangling to fit the current design.
Copy linkTweet thisAlerts:
@bc080400222authorNov 02.2010 — can any one tell me about the given below strings that is stored with serialize function.

a:3:{i:1;a:4:{s:4:"slno";i:1;s:9:"item_code";s:1:"1";s:9:"item_name";s:1:"1";s:8:"quantity";s:2:"10";}i:2;a:4:{s:4:"slno";i:2;s:9:"item_code";s:1:"2";s:9:"item_name";s:1:"2";s:8:"quantity";s:1:"5";}i:3;a:4:{s:4:"slno";i:3;s:9:"item_code";s:1:"3";s:9:"item_name";s:1:"3";s:8:"quantity";s:1:"5";}}

how i unserialize it?... there are three html form fields is stored in database one column.

Can any one tell me to retrieve these three fields into orginal forms?...

Waiting for good response...
Copy linkTweet thisAlerts:
@Jarrod1937Nov 02.2010 — You should just be able to run it through unserialize().
Copy linkTweet thisAlerts:
@bc080400222authorNov 02.2010 — Please look bellow attach picture i want to show left hand data in to original forms not with array printing form. Are you following?...

I hope you will understand now what i want. Please assist me with unserialize values into proper form.

[upl-file uuid=af429699-e646-405b-a6aa-b0e56527a8a9 size=18kB]11.png[/upl-file]
Copy linkTweet thisAlerts:
@bc080400222authorNov 02.2010 — when i run this query with this unserialize code:

<td align="center"><?php print_r (unserialize($ser_first2)); ?></td>

<td align="center"><?php print_r (unserialize($ser_last2)); ?></td>

<td align="center"><?php print_r (unserialize($ser_age2)); ?></td>

Then result is shown like this:

Array ( [0] => waqar [1] => vicky ) Array ( [0] => asif [1] => munna ) Array ( [0] => 20 [1] => 22 )

But i want original forms not with arrays. Can you please tell how i show these values into original form.

In other words i want to remove [ Array ] that are printed with each records.

I hope you will understand now?.
Copy linkTweet thisAlerts:
@Jarrod1937Nov 02.2010 — Sorry if it seems like we're not helping much, but your questions are not very clear. So are you just looking for ways to printout the array? If so checkout the foreach statement here:

http://www.phpf1.com/tutorial/php-print-array.html?page=2
Copy linkTweet thisAlerts:
@bc080400222authorNov 02.2010 — can you please only tell me one thing. My real problem is this:

After post the values. When i retrive these values with print_r function then below result will be shown.

[code=php]
<?php
$first = $_POST["first"];
$last = $_POST["last"];
$age = $_POST["age"];

$ser_first2 = serialize($first);
$ser_last2 = serialize($last);
$ser_age2 = serialize($age);
?>
<?php print_r (unserialize($ser_first2)); ?>
<?php print_r (unserialize($ser_last2)); ?>
<?php print_r (unserialize($ser_age2)); ?>
[/code]


Output:

Array ( [0] => waqar [1] => vicky ) Array ( [0] => asif [1] => munna ) Array ( [0] => 20 [1] => 22 )



i want to show this data in this form.

waqar asif 20

vicky munna 22

How i show values like this form? Can you please give me concept?

i think you understand now please.

waiting for your response.....
Copy linkTweet thisAlerts:
@Jarrod1937Nov 02.2010 — To do that you should loop through the arrays, the only issue is that you really should be storing all of this data in separate database fields/columns within a table and each record associated with one another (such as aperson's age and their name) in the same row of data. Reason being, is you can loop through your current arrays, but if one of the values is missing you will be messed up as you then lose your association you had between the values in the arrays (which is the index value they shared). However, with your current setup you can try something like:

[code=php]
<?php
// POST data, be sure to sanitize them to prevent persistant xss and sql injection attacks
$first = $_POST["first"];
$last = $_POST["last"];
$age = $_POST["age"];

// Serialized and placed into db
$ser_first2 = serialize($first);
$ser_last2 = serialize($last);
$ser_age2 = serialize($age);

// Strings which should be requested from the db and then unserialized
$unser_first=unserialize($ser_first2)
$unser_last=unserialize($ser_last2)
$unser_age=unserialize($ser_age2)

// Use a loop of some sort to go through the array, you should choose
// one as the "key" array to base your count off of
$startloop=0;
while($startloop < count(unser_first)){
echo 'firstname: '.$unser_first[$startloop].', lastname: '.$unser_last[$startloop].', and their age: '.$unser_age[$startloop];
$startloop++;
}
[/code]
Copy linkTweet thisAlerts:
@bc080400222authorNov 04.2010 — Thanks a lot jarrod i have don work sucessfully. I used separate three column in database table to store the three different variables values into them. and i also retrive them successfully. Can i store these three variables values { FirstName, LastName and Age } into database table one column and one row e.i { items_purchased } with serialize? and after doing this retrieve them with unserialize.

Can you help me with this matter more?

[code=php]

$first = $_POST["first"];
$last = $_POST["last"];
$age = $_POST["age"];

// Serialized and placed into db
$ser_first2 = serialize($first);
$ser_last2 = serialize($last);
$ser_age2 = serialize($age);
$item_purchased = serialize("$ser_first2"."$ser_last2"."$ser_age2");

[/code]


It is a simple idea. But not a perfect. If you have any idea please share it. Any help will be appretiate it...
×

Success!

Help @bc080400222 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...