<?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>Pontus Östlund &#187; Serendipity</title>
	<atom:link href="http://www.poppa.se/blog/tag/serendipity/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.poppa.se/blog</link>
	<description>My blog about web development and such</description>
	<lastBuildDate>Mon, 16 Jan 2012 00:38:45 +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>Merging associative arrays in PHP</title>
		<link>http://www.poppa.se/blog/merging-associative-arrays-in-php/</link>
		<comments>http://www.poppa.se/blog/merging-associative-arrays-in-php/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 22:16:50 +0000</pubDate>
		<dc:creator>Pontus</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Pike]]></category>
		<category><![CDATA[Serendipity]]></category>

		<guid isPermaLink="false">/blog/?p=254</guid>
		<description><![CDATA[It&#8217;s nice when serendipity is your friend! I was porting my Bitly class from Pike to PHP &#8211; I know there&#8217;s probably a hundred PHP classes already out there, but mine is better coded   &#8211; and noted by accident that I had used some Pike syntax in my PHP class but it was [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s nice when <a href="http://en.wikipedia.org/wiki/Serendipity">serendipity</a> is your friend! I was porting my <a href="http://bit.ly/">Bitly</a> class from <a href="http://pike.ida.liu.se">Pike</a> to <a href="http://php.net">PHP</a> &#8211; I know there&#8217;s probably a hundred PHP classes already out there, but mine is better coded <img src='http://www.poppa.se/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  &#8211; and noted by accident that I had used some Pike syntax in my PHP class but it was working anyway. So what was I doing? In Pike there&#8217;s separate data type for associative arrays called <code>mapping</code>. In Pike, in general, when merging two objects you just join them with a <code>+</code> sign. Thus merging two mappings you do like</p>
<pre><code lang="pike">
mapping m1 = ([ "key1" : "Value 1", "key2" : "Value 2" ]);
mapping m2 = ([ "key3" : "Value 3" ]);

write("My mapping: %O\n", m1 + m2);
//> My mapping: ([ /* 3 elements */
//>   "key1": "Value 1",
//>   "key2": "Value 2",
//>   "key3": "Value 3"
//> ])</code></pre>
<p>And I noted that I had done the same thing in PHP and the result was perfectly valid:</p>
<pre><code lang="php">
$a1 = array("key1" => "Value 1", "key2" => "Value 2");
$a2 = array("key3" => "Value 3");

echo "My mapping: ";
print_r($a1 + $a2);
//> My mapping: Array
//> (
//>     [key1] => Value 1
//>     [key2] => Value 2
//>     [key3] => Value 3
//> )</code></pre>
<p>This method doesn&#8217;t work on flat array though so there you&#8217;ll still have to use <code><a href="http://php.net/array_merge">array_merge()</a></code>, but pretty nice anyway.</p>
<p>And oh, the PHP Bitly class will be part of the new <a href="/blog/projects/#plib">PLib</a> release once done!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.poppa.se/blog/merging-associative-arrays-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

