<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Colin Jensen &#187; unserialize</title>
	<atom:link href="http://www.colinjensen.co.uk/blog/tag/unserialize/feed" rel="self" type="application/rss+xml" />
	<link>http://www.colinjensen.co.uk/blog</link>
	<description>Technology, Gadgets, Website Design and Development, Windows Mobile.</description>
	<lastBuildDate>Thu, 13 Aug 2009 17:02:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to convert an array or object into a string in php</title>
		<link>http://www.colinjensen.co.uk/blog/web-related/php/how-to-convert-an-array-or-object-into-a-string-in-php</link>
		<comments>http://www.colinjensen.co.uk/blog/web-related/php/how-to-convert-an-array-or-object-into-a-string-in-php#comments</comments>
		<pubDate>Sun, 09 Aug 2009 14:56:47 +0000</pubDate>
		<dc:creator>Colin Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[convert array to string]]></category>
		<category><![CDATA[convert object to string]]></category>
		<category><![CDATA[how to convert an array or object into a string]]></category>
		<category><![CDATA[php arrays]]></category>
		<category><![CDATA[php converting arrays]]></category>
		<category><![CDATA[php converting objects]]></category>
		<category><![CDATA[php objects]]></category>
		<category><![CDATA[php serialize]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[unserialize]]></category>

		<guid isPermaLink="false">http://www.colinjensen.co.uk/blog/?p=109</guid>
		<description><![CDATA[I am asked &#8220;How do I convert an array or object into a string&#8221; very frequently so I thought I would put a quick little tutorial together explaining the various different methods.
Converting an array OR object into a string(BEST METHOD)
We are going to use the functions serialize and unserialize.
These functions are very easy to use, [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>I am asked &#8220;How do I convert an array or object into a string&#8221; very frequently so I thought I would put a quick little tutorial together explaining the various different methods.</p>
<p><strong><span style="text-decoration: underline;">Converting an array OR object into a string(BEST METHOD)</span></strong></p>
<p>We are going to use the functions <a href="www.php.net/serialize">serialize</a> and <a href="www.php.net/unserialize">unserialize</a>.</p>
<p>These functions are very easy to use, for example converting an array or object you would do the following:</p>
<blockquote><p>
<code><br />
$string = serialize($objectorarray);<br />
</code>
</p></blockquote>
<p>Now to convert the string back into an object or array we would do:</p>
<blockquote><p>
<code><br />
$objectorarray = unserialize($string);<br />
</code>
</p></blockquote>
<p>It really is as simple as that, <strong>however sometimes the serialize function doesn’t do a good job at escaping characters so to get around this</strong> we do the following:</p>
<blockquote><p>
<code><br />
$string =base64_encode(serialize($objectorarray)); // This converts into a string</p>
<p>$objectorarray = unserialize( base64_decode($string)); // This converts a string into an array or object<br />
</code>
</p></blockquote>
<p><strong><span style="text-decoration: underline;">Converting an array into a string (OLD FASHIONED METHOD).</span></strong></p>
<p><a href="www.php.net/implode">Implode</a> and <a href="www.php.net/explode">Explode</a> can be used to convert an array.</p>
<p>Look at my example below.</p>
<blockquote><p>
<code><br />
$myarray[] = 'Apple';</p>
<p>$myarray[] = 'Orange';</p>
<p>$string = implode('|',$myarray);</p>
<p>echo $string; // This would return Apple|Orange<br />
</code>
</p></blockquote>
<p>Using the example above, the implode function takes two parameters.  First our separator and second our array.</p>
<p>To now convert the string BACK into an array we use explode. See the example below.</p>
<blockquote><p>
<code><br />
$string = 'Apple|Orange';</p>
<p>$myarray = explode('|',$string);</p>
<p>print_r($myarray);<br />
</code>
</p></blockquote>
<p>The explode function takes pretty much the same parameters as implode except it looks for a string rather then an array.</p>
<p><strong><span style="text-decoration: underline;">Why would you want to convert an array or object into a string?</span></strong></p>
<p>When saving vast amounts of data into a mysql table or even a text file, all you would need to do using the method above is save the string into the file or into a table row.  Very useful if you have plenty of dynamic data&#8230; No need to create lots of column names when you can save the object or array into a single row!</p>
<!-- google_ad_section_end --><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.colinjensen.co.uk%2Fblog%2Fweb-related%2Fphp%2Fhow-to-convert-an-array-or-object-into-a-string-in-php&amp;linkname=How%20to%20convert%20an%20array%20or%20object%20into%20a%20string%20in%20php"><img src="http://www.colinjensen.co.uk/blog/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.colinjensen.co.uk/blog/web-related/php/how-to-convert-an-array-or-object-into-a-string-in-php/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
