<?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; function</title>
	<atom:link href="http://blog.chuckcerrillo.com/tag/function/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>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>
	</channel>
</rss>
