<?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>Coder Snippets &#187; JSON</title>
	<atom:link href="http://codersnippets.com/category/web/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://codersnippets.com</link>
	<description>Useful tips and snippets I find or come up with.</description>
	<lastBuildDate>Fri, 04 Dec 2009 16:53:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Define an array of data with PHP and JSON</title>
		<link>http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/</link>
		<comments>http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 00:08:13 +0000</pubDate>
		<dc:creator>Sirbastian</dc:creator>
				<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://codersnippets.com/code/?p=32</guid>
		<description><![CDATA[Something I've always wanted was a way to store an array of data within a PHP define variable. 
The only solution I've come up with for this is to store JSON text in the define then decode it into an array when the data is needed.
My example will store some settings used to connect to [...]]]></description>
			<content:encoded><![CDATA[<p>Something I've always wanted was a way to store an array of data within a PHP define variable. </p>
<p>The only solution I've come up with for this is to store JSON text in the define then decode it into an array when the data is needed.</p>
<p>My example will store some settings used to connect to a database.</p>
<p>Firstly you've got to define DBCONFIG using JSON</p>
<pre class="brush: php">
     define('DBCONFIG', '{"aDbHost":"localhost","aDbUser":"root","aDbPass":"hello1","aDbName":"mydatabase","debugging":"false"}');
</pre>
<p>Load the JSON into an array using:</p>
<pre class="brush: php">
     $aConfigArray = json_decode(DBCONFIG, true);
     // true tells the function to return an array not an object
</pre>
<p>Now you can extract the array into seperate variables.</p>
<pre class="brush: php">
     extract($aConfigArray);
</pre>
<p>Once you've extracted all of the variables you can use them however you need.</p>
<pre class="brush: php">
$aDb = mysql_connect($aDbHost, $aDbUSer,  $aDbPass);
mysql_select_db($aDbName);
</pre>
<p>or</p>
<pre class="brush: php">
print 'Host: '. $aDbHost;
print 'User: '. $aDbUser;
print 'Pass: '. $aDbPass;
print 'DB Name: '. $aDbName;
</pre>
<p>Outputs:<br />
Host: localhost<br />
User: root<br />
Pass: hello1<br />
DB Name: mydatabase</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d32').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark this post</em></strong></a>
<br />
<div class="d32" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/&amp;submitHeadline=Define+an+array+of+data+with+PHP+and+JSON&amp;submitSummary=" rel="nofollow" title="Add to&nbsp;Buzz"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/buzz.png" title="Add to&nbsp;Buzz" alt="Add to&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/&amp;title=Define+an+array+of+data+with+PHP+and+JSON" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/&amp;title=Define+an+array+of+data+with+PHP+and+JSON" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/&amp;title=Define+an+array+of+data+with+PHP+and+JSON" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/&amp;title=Define+an+array+of+data+with+PHP+and+JSON" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/&amp;title=Define+an+array+of+data+with+PHP+and+JSON" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/" rel="nofollow" title="Add to&nbsp;Tip'd"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/tipd.png" title="Add to&nbsp;Tip'd" alt="Add to&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Define+an+array+of+data+with+PHP+and+JSON+@+http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/&amp;t=Define+an+array+of+data+with+PHP+and+JSON" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://codersnippets.com/code/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d32').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://codersnippets.com/web/define-an-array-of-data-with-php-and-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

