<?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>Chuck&#039;s Musings &#187; howto</title>
	<atom:link href="http://blog.chuckcerrillo.com/tag/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.chuckcerrillo.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 21 Feb 2010 18:43:40 +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: Insert a new field into a dynamic array</title>
		<link>http://blog.chuckcerrillo.com/2009/09/how-to-insert-a-new-field-into-a-dynamic-array/</link>
		<comments>http://blog.chuckcerrillo.com/2009/09/how-to-insert-a-new-field-into-a-dynamic-array/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 01:59:14 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=103</guid>
		<description><![CDATA[Inserting new fields into a dynamic array.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been busy with work &#8211; deadlines and all, so it&#8217;s been a while since my last entry. For now I&#8217;ll just share a quick tip. How to <em>Insert a new field into a dynamic array</em>.</p>
<p>If you&#8217;ve been reading the code snippets I&#8217;ve been posting thus far, you&#8217;ll notice that I am doing this thing a lot. Anyway, here&#8217;s a little background on this situation.</p>
<p>Let&#8217;s say you just retrieved records from your database table and stored them into an associative array named <code class="codecolorer php default"><span class="php"><span style="color: #000088;">$results</span></span></code>, now you want to insert an extra field for each row but you don&#8217;t know how to to assign them since you don&#8217;t know how to walk through the array.</p>
<p>Let&#8217;s say you have this array:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Array<br />
(<br />
&nbsp; &nbsp; [0] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Chuck<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1984-09-15<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [1] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Blah<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2001-01-01<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [2] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; New user<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2002-10-30<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [3] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 4<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Random guy<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1992-07-21<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
)</div></div>
<p>This means, each row has the following fields &#8211; user_id, user_name, user_level, and birthdate.<br />
Now, let&#8217;s say we wanted to create a new field named &#8220;age&#8221; and store it in the same array. Here&#8217;s my preferred method of doing this:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> get_age<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'birthdate'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Take note that to maintain the abstraction of the code, I just called a function named <code class="codecolorer php default"><span class="php">get_age<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>. Remember that there is no such function so you&#8217;ll have to code that in to make it work. For completeness sake, here&#8217;s how I&#8217;d probably do it:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Accepts 2 parameters, $bday and $now. $bday is of </span><br />
<span style="color: #666666; font-style: italic;">// the 'YYYY-MM-DD' format. The result is based on the </span><br />
<span style="color: #666666; font-style: italic;">// difference between $now and $bday converted to</span><br />
<span style="color: #666666; font-style: italic;">// years. $now is optional. If it is not supplied, it uses </span><br />
<span style="color: #666666; font-style: italic;">// the current time. This is not exactly accurate since I</span><br />
<span style="color: #666666; font-style: italic;">// just indiscriminately used 365.25 days in my calculation.</span><br />
<span style="color: #666666; font-style: italic;">// To improve, I'll have to detect leap years and just</span><br />
<span style="color: #666666; font-style: italic;">// add 1 day to that, instead of 1/4 days to every year.</span><br />
<span style="color: #000000; font-weight: bold;">function</span> get_age<span style="color: #009900;">&#40;</span><span style="color: #000088;">$bday</span><span style="color: #339933;">,</span> <span style="color: #000088;">$now</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$bday</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bday</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$now</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$now</span><span style="color: #009900;">&#41;</span>?<a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$now</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><a href="http://www.php.net/mktime"><span style="color: #990000;">mktime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <a href="http://www.php.net/floor"><span style="color: #990000;">floor</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$now</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$bday</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>60<span style="color: #339933;">*</span>60<span style="color: #339933;">*</span>24<span style="color: #339933;">*</span>365<span style="color: #339933;">.</span>25<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Anyway, after doing all that, you will get this result:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Array<br />
(<br />
&nbsp; &nbsp; [0] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Chuck<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1984-09-15<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 24<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [1] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Blah<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2001-01-01<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 8<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [2] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; New user<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 2002-10-30<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 6<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
&nbsp; &nbsp; [3] =&gt; Array<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_id] =&gt; 4<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_name] =&gt; Random guy<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [user_level] =&gt; 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [birthdate] =&gt; 1992-07-21<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] =&gt; 17<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
<br />
)</div></div>
<p>As an aside, you could probably use <a href="http://us.php.net/manual/en/function.array-walk.php"><code class="codecolorer php default"><span class="php"><a href="http://www.php.net/array_walk"><span style="color: #990000;">array_walk</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code></a> to achieve something similar, but this method is way simpler. I hope this has been helpful to you!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/09/how-to-insert-a-new-field-into-a-dynamic-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
