<?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; PHP</title>
	<atom:link href="http://blog.chuckcerrillo.com/tag/php/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>
		<item>
		<title>Juggling time zones</title>
		<link>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/</link>
		<comments>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 00:30:14 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[gmdate]]></category>
		<category><![CDATA[gmt]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[offsets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[strtotime]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[timezones]]></category>
		<category><![CDATA[utc]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=100</guid>
		<description><![CDATA[If you're thinking of writing calendars, you better watch out for timezones.]]></description>
			<content:encoded><![CDATA[<p>Handling times for different timezones is one of the usual things I had to face with every project that has in one way or the other involves the element of time.</p>
<p>The usual projects I work on store dates into a <code class="codecolorer sql default"><span class="sql">datetime</span></code> field in the database. This means that the date must use the <code class="codecolorer text default"><span class="text">YYYY-MM-DD HH:MM:SS</span></code> format (e.g. 2009-09-03 07:45:00).</p>
<p>One of the first things you&#8217;d want to do is to let the user specify a timezone setting for that user account (assuming this is in a multi-user environment, as most web applications are). You could view all the timezones using this code:</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;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span>DateTimeZone<span style="color: #339933;">::</span><span style="color: #004000;">listAbbreviations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$timezone_abbreviation</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$timezones</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezones</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timezone_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$timezone_abbreviation</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' - '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timezone_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ul&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;li&gt;Uses DST: '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dst'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">TRUE</span>?<span style="color: #0000ff;">'Yes'</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'No'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;li&gt;Offset: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'offset'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ul&gt;&lt;/li&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span></div></div>
<p>You could modify the code above to create a drop-down list of the different timezones to make it easier for the user to pick a timezone (because simply writing GMT+8 or UTC+8 might be confusing for some people).</p>
<p>Next, we will have to convert all of our stored dates into a single timezone, for simplicity, I use GMT/UTC as a reference timezone (since it is easy to add timezone offsets to it). If you store your dates in a <code class="codecolorer sql default"><span class="sql">datetime</span></code> field, you can perform the following conversion after retrieving the timestamp from the database:</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;">// let's assume $parsedtime is the entry we have in the database</span><br />
<span style="color: #666666; font-style: italic;">// and $offset is the timezone offset we got based on the user's settings</span><br />
<span style="color: #000088;">$parsedtime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2009-09-15 12:00:00'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> 8<span style="color: #339933;">*</span>60<span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this is GMT +8</span><br />
<span style="color: #666666; font-style: italic;">// get the GMT counterpart of the parsedtime</span><br />
<span style="color: #000088;">$unix_time</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;">$parsedtime</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' GMT'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/gmdate"><span style="color: #990000;">gmdate</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'m d Y h:iA'</span><span style="color: #339933;">,</span><span style="color: #000088;">$unix_time</span><span style="color: #339933;">-</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// displays: 09 15 2009 04:00AM</span></div></div>
<p>So that means, 12nn of September 15, 2009 is 4am of September 15, 2009 in GMT/UTC.</p>
<p>Let&#8217;s say we had a Calendar application, and you wanted to set an alarm for 6pm on that same day. You could do the something like the this (assuming you wrote some sort of class for this thing):</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: #000088;">$reminder</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Reminder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2009-09-15 18:00:00'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Dinner with the family'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$reminder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timeLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Since the current date and time was 12nn of September 15, 2009, this means that the above reminder will display &#8220;6 hours left&#8221;, depending on how the <code class="codecolorer php default"><span class="php">timeLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> method is formatted.</p>
<p>If we didn&#8217;t convert the date into a standard timezone, this will force our functions to use the server&#8217;s timezone setting and that might not be what you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Sorting uneven 2-dimensional arrays</title>
		<link>http://blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/</link>
		<comments>http://blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 16:09:24 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[2-dimensional arrays]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[multisort]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=60</guid>
		<description><![CDATA[Is array_multisort() not working for you? You might want to try this.]]></description>
			<content:encoded><![CDATA[<p>I was working on a search function for the module I was assigned to. The contents stored in the database isn&#8217;t plaintext, but rather encoded in base64. There&#8217;s currently no native MySQL functions to handle base64 data so that means I can&#8217;t make inline text and regex comparisons.</p>
<p>I thought hard about it and came up with 2 solutions:</p>
<ol>
<li>Save the data in plaintext instead and use heavy data validation</li>
<li>Retrieve all rows and store them in a PHP array and do my regex magic there</li>
</ol>
<p>Option 1 was out of the question, because in every part of the module, we&#8217;ve made sure to protect against XSS (<a href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross site scripting</a>). Going with this option will always risk creating a security hole regardless of how carefully I validate the supplied data.</p>
<p>Option 2 would be more secure and would allow me to apply my regex skills to the fullest extent, but it would cost more memory overhead because I&#8217;d have to retrieve the rows, store them into memory while doing the pattern matching&#8230;</p>
<p>Until I thought of <em>option 3 </em>(well, more like option 2.1). What I&#8217;d do is to limit the fetched rows by imposing more conditions, say for example, <em>retrieve the rows that the currently logged user is only allowed to see</em>. Then perform my regex thingamabob on those filtered results.</p>
<p>I had another problem though, doing this means I have to work on some sort of system to weigh the <em>relevance</em> of the results. Since this <em>relevance value</em> is computed after the database retrieval, this means I&#8217;ll be adding it into an extra column in the result array. The result array I am supposed to work on is a multidimensional array. It would have a similar structure as this example:</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: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Chuck'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Blue'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Rupert'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Black'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Cerrillo'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Red'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Abarro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'White'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The above array is a 2-dimensional array.  This means that <code class="codecolorer php default"><span class="php"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></span></code> will display <em><strong>&#8220;Chuck&#8221;</strong></em>.</p>
<p>Anyway, as I earlier mentioned, I am supposed to add a <em>relevance value</em> to each row (or what I&#8217;d like to call <em>weight</em>). In addition, I might add some custom fields to some rows. Here&#8217;s how that array would look like with the additional fields and arbitrary weight values. (take note of the custom field added in line #6)</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Chuck'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Blue'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'custom'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Boo!'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Rupert'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Black'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Cerrillo'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Red'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'weight'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Abarro'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'color'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'White'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>By my weight system, the &#8220;heavier&#8221; a row is, the more relevant it is to the search query. So I should sort it by weight in a descending order. </p>
<p>PHP has a native array function called <code class="codecolorer php default"><span class="php"><a href="http://www.php.net/array_multisort"><span style="color: #990000;">array_multisort</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>, so I tried that one. It doesn&#8217;t work with my array because the array sizes are not the same. I gave up a while after looking for workarounds to get it to work with that function, so I decided to write my own sorting function for arrays like these.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br /></div></td><td><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;">/*<br />
* 2-Dimensional Array Sort by Chuck Cerrillo (August 26, 2009)<br />
* Sorts a 2-dimensional array according to a given field/index.<br />
* This is particularly useful for sorting post-processed MySQL data (i.e. where <br />
* custom fields have been added AFTER the retrieval of table data, and you'd need <br />
* to resort the results).<br />
*<br />
* For more information on how to use this array, please visit my blog entry at<br />
* http:///blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">function</span> array_2dsort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sort</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// First we grab the index names of the sub-array</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// by walking through the array</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$subarrayindex</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$subarray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subarray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subarray</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$index</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Store the fieldnames into the $fieldnames array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$fieldnames</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: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$subarray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// We then store the field names into a temporary array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#123;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$subarrayindex</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Sort the temporary array based according to the $sort</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// argument, and maintain index associations by using </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// asort() or arsort()</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sort</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #990000;">asort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #990000;">arsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$$field</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Now we recreate a new array with the desired order</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldnames</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$fieldname</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$fieldindex</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fieldindex</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$rowindex</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$rowvalue</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#123;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$index</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$result</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: #000088;">$fieldname</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$array</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: #000088;">$fieldname</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now this one works perfectly according to my needs. This function takes in 3 parameters,</p>
<ul>
<li><code class="codecolorer php default"><span class="php"><span style="color: #000088;">$array</span></span></code> &#8211; the array to sort (required)</li>
<li><code class="codecolorer php default"><span class="php"><span style="color: #000088;">$field</span></span></code> &#8211; the field or index to sort by (required)</li>
<li><code class="codecolorer php default"><span class="php"><span style="color: #000088;">$sort</span></span></code> &#8211; the sorting order, either asc or desc, defaults to asc (optional)</li>
</ul>
<p>Given the above array, I would call my function this way &#8211; <code class="codecolorer php default"><span class="php"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> array_2dsort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'weight'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>. If you would do a <code class="codecolorer php default"><span class="php"><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code>, it would give you this: (note that I didn&#8217;t specify &#8216;desc&#8217; as my sorting order)</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990000;">Array</span><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Rupert<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Black<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 20<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Chuck<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Blue<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Boo<span style="color: #339933;">!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>3<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 40<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Abarro<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> White<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span>2<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>weight<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">50</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Cerrillo<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Red<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#91;</span>custom<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<br />
<span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>This function <em>automagically</em> adds blank fields to rows that do not contain them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/08/sorting-uneven-2-dimensional-arrays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Regex saves the day, again</title>
		<link>http://blog.chuckcerrillo.com/2009/08/regex-saves-the-day-again/</link>
		<comments>http://blog.chuckcerrillo.com/2009/08/regex-saves-the-day-again/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 22:21:08 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[search tool]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=44</guid>
		<description><![CDATA[Searching is easier with regex... Good thing MySQL supports that in SQL queries too!]]></description>
			<content:encoded><![CDATA[<p>I was supposed to do a &#8220;search&#8221; feature and a &#8220;related items&#8221; feature for the module I was assigned to. This was were my regex skills came in handy yet again.</p>
<p>I made a very barebone prototype of it, which takes some input text for the search query against some stored content, in this case, a full-text article (although the final version should be against a database).</p>
<p>The prototype I made is still very rough around the edges. What it does is that it extracts all keywords from the search query using this expression:</p>
<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: #000088;">$regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/\b([\p{L}|\p{Ll}|\p{Lu}|_]+?)\b/i'</span><span style="color: #339933;">;</span></div></div>
</p>
<p>That rule can be translated as follows:</p>
<p style="padding-left: 30px;"><em><span style="color: #993366;">take any combination of unicode characters and underscores that are enclosed in word boundaries </span></em></p>
<p>Having done that, I place all matched data into an array and call that my &#8220;keywords&#8221; array, to be used later.</p>
<p>The next thing I did was to chop the article down into smaller pieces, currently by sentence (I only used periods as a delimiter, I should probably include other punctuation marks I guess). Then I ran <em>preg_match()</em> through each piece to quickly check for matches for any of the keywords. These results are then compiled.</p>
<p>In each compiled piece, I assign a corresponding weight. This weight is my arbitrary way of picking the best match. My currently implementation <em>sets weight to be equal to the number of characters in the piece that are matched with keywords</em>. I still have to refine these rules later on.</p>
<p>When all pieces have their weight assigned, I sort them according to weight in descending order (highest to lowest weight). Then I display the results and highlight the matched characters.</p>
<p>Here&#8217;s the prototype I made: <a href="http://thedirtlab.chuckcerrillo.com/php-and-mysql/search.php">search tool</a>.</p>
<p>I&#8217;m still thinking of the refinements I could make so I&#8217;ll just post them here as I go along.</p>
<p>Also, it&#8217;s good to know that MySQL supports regex in your SQL queries, this should save me a lot of time!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/08/regex-saves-the-day-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
