<?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>www.post-hipster.com &#187; php</title>
	<atom:link href="http://www.post-hipster.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.post-hipster.com</link>
	<description>A Shady Lane, everybody wants one</description>
	<lastBuildDate>Mon, 22 Aug 2011 12:00:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Elegant little PHP JSON encoder</title>
		<link>http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/</link>
		<comments>http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 14:18:52 +0000</pubDate>
		<dc:creator>chip</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.post-hipster.com/?p=35</guid>
		<description><![CDATA[update: Jay Williams has flushed this out to be a lot more useful. Check out his rendition here. &#8230;so I wrote (what I think is a) clever little function (well, two functions) to take a complex php variable and turn it into a json-ized string, ready to be passed back to javascript. It works on [...]]]></description>
			<content:encoded><![CDATA[<p><strong>update: <a title="Jay's Pictures" href="http://photoblog.dview.us/" target="_blank">Jay Williams</a> has flushed this out to be a lot more useful. Check out his rendition <a title="New Hotness" href="http://pastie.textmate.org/398110" target="_blank">here</a>.</strong></p>
<p>&#8230;so I wrote (what I think is a) clever little function (well, two functions) to take a complex php variable and turn it into a json-ized string, ready to be passed back to javascript. It works on the principle that json really only has a couple rules if text formatting: strings go inside double quotes, with both slashes and double-quotes escaped, iterative arrays are comma delimited inside brackets [obj1,obj2], and associative arrays go inside curly-brackets {key:val,key:val}. Using these three points, and a little bit (ok, a lot of) recursion, and voila, an elegant little function.</p>
<p>Of course, this doesn&#8217;t handle unicode, or any really special cases, but if you&#8217;re looking for a basic object parser, and you dont&#8217; have access to php 5.2, which has it built into the language, it&#8217;ll do the trick&#8230;</p>
<pre>/**
 * input an object, returns a json-ized string of said object
 * @return
 * @param $obj Object
 */
function php_json_encode($obj) {
	if (is_array($obj)) {
		if (array_is_associative($obj)) {
			$arr_out = array();
			foreach ($obj as $key=&gt;$val) {
				$arr_out[] = '"' . $key . '":' . php_json_encode($val);
			}
			return '{' . implode(',', $arr_out) . '}';
		} else {
			$arr_out = array();
			$ct = count($obj);
			for ($j = 0; $j &lt; $ct; $j++) {
				$arr_out[] = php_json_encode($obj[$j]);
			}
			return '[' . implode(',', $arr_out) . ']';
		}
	} else {
		if (is_int($obj)) {
			return $obj;
		} else {
			$str_out = stripslashes(trim($obj));
			$str_out = str_replace(array('"', '', '/'), array('\"', '\', '/'), $str_out);
			return '"' . $str_out . '"';
		}

	}
}

function array_is_associative($array) {
	$count = count($array);
	for ($i = 0; $i &lt; $count; $i++) {
		if (!array_key_exists($i, $array)) {
			return true;
		}
	}
	return false;
}</pre>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=www.post-hipster.com&amp;siteurl=http%3A%2F%2Fwww.post-hipster.com%2F&amp;linkname=Elegant%20little%20PHP%20JSON%20encoder&amp;linkurl=http%3A%2F%2Fwww.post-hipster.com%2F2008%2F02%2F15%2Felegant-little-php-json-encoder%2F">Share/Save</a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://www.post-hipster.com/2008/02/15/elegant-little-php-json-encoder/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

