<?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>#Adam.Blog# &#187; PHP</title>
	<atom:link href="http://blog.adampresley.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.adampresley.com</link>
	<description>I&#039;m bringin&#039; nerdy back.</description>
	<lastBuildDate>Fri, 03 Sep 2010 15:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><atom:link rel="hub" href="http://github.com/tonyg/rabbithub"/>		<item>
		<title>Sorting a Complex Array of Products in PHP</title>
		<link>http://blog.adampresley.com/2009/sorting-complex-array-of-products-in-php/</link>
		<comments>http://blog.adampresley.com/2009/sorting-complex-array-of-products-in-php/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 16:39:30 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Sorting]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=679</guid>
		<description><![CDATA[I&#8217;ve blogged twice recently about sorting an array of structures in ColdFusion, using both pure CF, and dipping into Groovy. This time we&#8217;ll look at how we can easily do the same task in PHP. In this post I will take the same problem, a complex array containing a structure (in PHP, an associative array), [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve blogged twice recently about sorting an array of structures in ColdFusion, using both pure CF, and dipping into Groovy. This time we&#8217;ll look at how we can easily do the same task in PHP. In this post I will take the same problem, a complex array containing a structure (in PHP, an associative array), and sort it. The sort criteria in this case is that if the product IDs are the same, we compare the prices of the products and sort there. So we have a two-level sort: Product ID, then price.</p>

<p>To do this in PHP the method <strong>usort</strong> will come in handy. The <strong>usort</strong> method takes two parameters. The first parameter is the array to be sorted (passed by reference, so make sure you keep a copy if you need the original), and a function to compare two items.</p>

<p>The comparator function simply takes two items. It is up to us to compare them how we like, and return zero (0) for equal, one (1) for the first item being greater (or after) the second item, or negative one (-1) for the first item coming before the second item, or less than. Let&#8217;s take a look at how that function would work.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> comparator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$productId1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$productId2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$price1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$price2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$productId1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$productId2</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$price1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$price2</span><span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$price1</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$price2</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$productId1</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$productId2</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>As you can see, we compare item $a and item $b. If the the two product IDs are the same, we&#8217;ll compare prices. To use this is quite easy. You pass an array into the <strong>usort</strong> method, and watch the magic happen. This is how that works.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> comparator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$productId1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$productId2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$price1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$price2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$productId1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$productId2</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$price1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$price2</span><span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$price1</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$price2</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$productId1</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$productId2</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$unsorted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #3&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">6.95</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #4&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">6.75</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #1&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">10.95</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #2&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">11.95</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #3&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">5.95</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sorted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$unsorted</span><span style="color: #339933;">;</span>
<span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorted</span><span style="color: #339933;">,</span> comparator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Notice how we create a copy of <em>$unsorted</em> as a variable named <em>$sorted</em> first. This is because the <strong>usort</strong> method works by reference, and will modify the original array. So we need a copy first to preserver the original (unless of course you don&#8217;t care about the original).</p>

<p>And that is all! Below is the code sample in full. Cheers, and happy coding!</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> comparator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$productId1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$productId2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$price1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$price2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$productId1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$productId2</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$price1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$price2</span><span style="color: #009900;">&#41;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span>
				<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$price1</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$price2</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$productId1</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$productId2</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$unsorted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #3&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">6.95</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #4&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">6.75</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #1&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">10.95</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #2&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">11.95</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;productId&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Widget #3&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;price&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">5.95</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;discounted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$sorted</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$unsorted</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorted</span><span style="color: #339933;">,</span> comparator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	&lt;title&gt;Sorting&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
	&lt;h1&gt;Sorting&lt;/h1&gt;
&nbsp;
	&lt;strong&gt;Unsorted:&lt;/strong&gt;&lt;br /&gt;
	&lt;ul&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$unsorted</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;li&gt;<span style="color: #009933; font-weight: bold;">%s</span> (<span style="color: #009933; font-weight: bold;">%s</span>) - <span style="color: #009933; font-weight: bold;">%s</span>&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/ul&gt;
	&lt;br /&gt;
&nbsp;
	&lt;strong&gt;Sorted:&lt;/strong&gt;&lt;br /&gt;
	&lt;ul&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorted</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;li&gt;<span style="color: #009933; font-weight: bold;">%s</span> (<span style="color: #009933; font-weight: bold;">%s</span>) - <span style="color: #009933; font-weight: bold;">%s</span>&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;productId&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;price&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/ul&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>



<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2009/sorting-complex-array-of-products-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple LRU Cache Class in PHP</title>
		<link>http://blog.adampresley.com/2009/simple-lru-cache-class-in-php/</link>
		<comments>http://blog.adampresley.com/2009/simple-lru-cache-class-in-php/#comments</comments>
		<pubDate>Wed, 27 May 2009 22:58:33 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=398</guid>
		<description><![CDATA[In my ongoing effort to further enhance my PHP framework I started recently working on a caching mechanism. For the time being I am using the PEAR library&#8217;s Cache_Lite (I&#8217;ve blogged on this before, so check it out) to handle the details of persisting the cache data, locking, and all that fun jazz. Cache_Lite, however, [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>In my ongoing effort to further enhance my PHP framework I started recently working on a caching mechanism. For the time being I am using the PEAR library&#8217;s <a href="http://pear.php.net/package/Cache_Lite" target="_blank">Cache_Lite</a> (I&#8217;ve blogged on this before, so <a href="http://blog.adampresley.com/2007/caching-in-php-cache_lite/" target="_blank">check it out</a>) to handle the details of persisting the cache data, locking, and all that fun jazz. Cache_Lite, however, is a pretty simple library and does not implement any particular algorithm regarding how much cache to keep, or what stays in cache and what goes. It offers an expiration mechanism, but that&#8217;s about it.So with that in mind I set off to learn about various caching and paging algorithms. They range, I found out, from simple to complex.</p>

<p><span id="more-398"></span></p>

<p>In this blog entry I will demonstrate the creation of a very simple LRU, or Least Recently Used, caching algorithm. I will provide downloads for PHP source code at the end of this entry.</p>

<p>First let&#8217;s talk about how the algorithm works. The concept behind LRU is that you have a finite set of <em>slots</em> in some container. Our container will be an array for simplicity. The LRU algorithm works off the premise that you keep only the most recently used items in your container, and those that get accessed are moved to the top of the array. Adding a new item to the container will be put in at the top of the array, and all subsequent items are shifted down. Any item at the bottom of the array is dropped off from the container.</p>

<p>Items in the LRU array are going to be stored as a structure (array in PHP) consisting of a <em>key</em> and <em>element</em>, which we will call a <em>node</em>. The <em>element</em> is simply whatever data is to be stored. The <em>key</em> is the identifier for the data element, and how we are able to insert and retrieve elements, or items, from the array.</p>

<p>There are two primary operations to supprt this: <strong>get</strong> and <strong>put</strong>. Let&#8217;s start with <strong>put</strong> as it is the simplest in concept. The first thing the <strong>put</strong> operation must do is <em>evict</em> any element at the bottom of the array, then shift all of the elements above down one notch. So, for example, of you have an array of 5 slots, and you are adding a new element, you will remove the last item in the array (slot 4), then shift items in slots 0 through 3 down into slots 1 through 4. From here you then insert the new item into slot 0.Â  Let&#8217;s take a look at how that works.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> put<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__evict<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__container<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__createNode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __evict<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$newPosition</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__container<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				<span style="color: #666666; font-style: italic;">// Drop off the element in the last slot.</span>
	<span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__container<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// Shift every element down one, and insert blank at the top.</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __createNode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;key&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;element&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$element</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Ok, that was easy enough. Now let&#8217;s talk about the <strong>get</strong> operation. <strong>Get</strong> starts with taking a single argument called <em>key</em>. It then attempts to find the correct node by looping over the container array, retrieving a node each iteration, then comparing the node&#8217;s key to the argument key. If we have a match we want to MOVE this node to the <strong>top</strong> of the array since it is the most recently used node. Once this is done we return the node. Here&#8217;s what that looks like.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__maxElements<span style="color: #339933;">;</span> <span style="color: #000088;">$index</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__container<span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&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;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__moveToHead<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__stats<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cacheHits&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;element&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__stats<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cacheMisses&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __moveToHead<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_splice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__container<span style="color: #339933;">,</span> <span style="color: #000088;">$index</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__container<span style="color: #339933;">,</span> <span style="color: #000088;">$element</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Using this class is pretty easy, so here is a simple test that provides a series of data items, and a comma-delimited pattern of indexes to those data items. The code then iterates over this list, simulating some part of a program that is being asked for some data, and it attempts to get the data from the cache. If it does not have it the code then puts the data into the cache.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
&nbsp;
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LRUCache.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$lru</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LRUCache<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;This is item 0&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;This is item 1&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;This is item 2&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;This is item 3&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;This is item 4&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;This is item 5&quot;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$testPattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;0,2,4,1,0,5,3,1,5,3,0,1,5,2,5,1,4&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Implement test pattern.</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$testPattern</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lru</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;strong&gt;Cache hit.&lt;/strong&gt; - <span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;strong&gt;Cache miss&lt;/strong&gt; for key <span style="color: #009933; font-weight: bold;">%s</span>. Adding <span style="color: #009933; font-weight: bold;">%s</span> to cache.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$index</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$lru</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">put</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lru</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCacheStats</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hits: <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cacheHits&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Misses: <span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;cacheMisses&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>


<p>Go ahead, mess with the test pattern and see how many cache hits vs cache misses there are. Kinda neat.</p>

<p><a href="http://www.adampresley.com/downloads/code/php/LRUCache.zip">Download the class code here!</a></p>

<p>Happy coding!</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2009/simple-lru-cache-class-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful Date/Time Class for PHP</title>
		<link>http://blog.adampresley.com/2009/useful-datetime-class-for-php/</link>
		<comments>http://blog.adampresley.com/2009/useful-datetime-class-for-php/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 05:56:03 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=361</guid>
		<description><![CDATA[As programmers we should always strive to better ourselves by improving our craft by taking pride that what we build is not only useful, but can continue to be useful for a long time to come. Part of this strategy involves writing code that exhibits resuse, high cohesion, and loose coupling. Therefore every developer should, [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>As programmers we should always strive to better ourselves by improving our craft by taking pride that what we build is not only useful, but can continue to be useful for a long time to come. Part of this strategy involves writing code that exhibits resuse, high cohesion, and loose coupling. Therefore every developer should, over time, develop, or at least acquire, sets of tools and code to help achive this goal.</p>

<p>Concepts like frameworks and design patterns help us achieve this. I have been working a few contract projects in <a href="http://www.php.net" target="_blank">PHP</a> lately and have had many opportunities to tweak, fine tune, and improve my home-brewed PHP framework. One of these days I will share this work as a complete framework for download and use, but for now it will simply have to suffice to drop small pieces to the public.</p>

<p><span id="more-361"></span></p>

<p>One common task a developer will find themselves doing is manipulating dates. This could be for the purposes of display, SQL queries, or a plethora of other needs, but regardless of the reason having a useful set of classes and functions for manipulating dates and times is handy.</p>

<p>In my framework I have built a simple class to do the most common date tasks that I find myself repeating over and over. In my case I have opted for a class that implements all static methods, so this is not meant to be instantiated. And it is also only intended for PHP 5.2 and higher.</p>

<p>Most the of the functions are pretty simple to follow, and there is ample documentation attached. A couple of items worth noting are the <a href="http://www.php.net/manual/en/function.date-parse.php" target="_blank">date_parse</a> and the <a href="http://www.php.net/manual/en/function.mktime.php" target="_blank">mktime</a> functions. In PHP, the <a href="http://www.php.net/manual/en/function.date.php" target="_blank">date</a> function is what allows us to format a date into whatever string we desire to craft. The <strong>mktime</strong> function allows us to specify the values for each part of a date and time string, such as the day, month, year, hour, and so on.</p>

<p>What is usually tricky is making functions to format a date from various sources. For example, a SQL date string usually looks like <em>2009-02-01</em>, where a user in the United States might enter a value on a form like <em>2/1/2009</em>. This is where the <strong>date_parse</strong> function comes in handy. It is smart enough to take these various types of input and split them into their appropriate parts. The end result is an array of its parts. For example.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;02/01/2009&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$split</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date_parse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// $split[&quot;month&quot;] = &quot;02&quot;</span>
<span style="color: #666666; font-style: italic;">// $split[&quot;day&quot;] = &quot;01&quot;</span>
<span style="color: #666666; font-style: italic;">// $split[&quot;year&quot;] = &quot;2009&quot;</span></pre></td></tr></table></div>


<p>As we can see the date has been split up into each of its parts. We can then take these parts and pass them to <strong>mktime</strong> to build the value necessay to pass to the <strong>date</strong> function for formatting.</p>

<p>I have linked to a download containing my date/time class, as well as the documentation for it. Enjoy, and happy coding!</p>

<p><a href="http://www.adampresley.com/downloads/code/php/dateTimeClass.zip">Download code</a></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2009/useful-datetime-class-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing PHP-style Placeholders for SQL Scripts</title>
		<link>http://blog.adampresley.com/2008/parsing-php-style-placeholders-for-sql-scripts/</link>
		<comments>http://blog.adampresley.com/2008/parsing-php-style-placeholders-for-sql-scripts/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 05:12:49 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=276</guid>
		<description><![CDATA[If you&#8217;ve ever used PHP, you&#8217;ve probably used, or at least seen, the PRINTF and/or SPRINTF functions. These oldie, but goodie, functions are stolen&#8230; err&#8230; borrowed from the old C language world. Both functions support the abililty to have placeholders, which when evaluated, are replaced with the value in a comma-delimited list in the appropriate [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever used PHP, you&#8217;ve probably used, or at least seen, the <a href="http://us2.php.net/manual/en/function.printf.php" target="_blank"><em>PRINTF</em></a> and/or <a href="http://us2.php.net/manual/en/function.sprintf.php" target="_blank"><em>SPRINTF</em></a> functions. These oldie, but goodie, functions are stolen&#8230; err&#8230; borrowed from the old <a href="http://en.wikipedia.org/wiki/C_language" target="_blank">C language</a> world. Both functions support the abililty to have placeholders, which when evaluated, are replaced with the value in a comma-delimited list in the appropriate position. For example:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;My name is <span style="color: #009933; font-weight: bold;">%s</span>. I am %i years old&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$age</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>This code, given that <strong>$name</strong> equals &#8220;Adam&#8221;, and <strong>$age</strong> equals &#8220;30&#8243; will output &#8220;My name is Adam. I am 30 years old&#8221;. A nifty feature.</p>

<p>Below is a bit of code that provides a class that will take a given input SQL string, an array of arguments, and spit out a parsed SQL string. An example of usage would look like so:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$userId</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$parser</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SqlScriptParser<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT name, email, active FROM users WHERE userId=%i&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userId</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$parser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>The resulting string would look like:</p>

<p><strong>SELECT name, email, active FROM users WHERE userId=30</strong></p>

<p>The code: <a href="http://www.adampresley.com/downloads/code/php/sqlScriptParser.zip">http://www.adampresley.com/downloads/code/php/sqlScriptParser.zip</a></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2008/parsing-php-style-placeholders-for-sql-scripts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New Tutorial on Complex Objects in PHP Sessions</title>
		<link>http://blog.adampresley.com/2008/new-tutorial-on-complex-objects-in-php-sessions/</link>
		<comments>http://blog.adampresley.com/2008/new-tutorial-on-complex-objects-in-php-sessions/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 03:42:39 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=154</guid>
		<description><![CDATA[For those PHP programmers out there new to the language I have posted a new tutorial (just a little one ) on how to store and retrieve complex objects (classes) in PHP session ($_SESSION). You can find it under Software Development, or go straight to it here. Cheers! No related posts.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>For those PHP programmers out there new to the language I have posted a new tutorial (just a little one <img src='http://blog.adampresley.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) on how to store and retrieve complex objects (classes) in PHP session ($_SESSION). You can find it under <a href="http://blog.adampresley.com/software-development/">Software Development</a>, or go straight to it <a href="http://blog.adampresley.com/software-development/complex-objects-in-php-sessions/">here</a>. Cheers!</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2008/new-tutorial-on-complex-objects-in-php-sessions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My First Plugin</title>
		<link>http://blog.adampresley.com/2007/my-first-plugin/</link>
		<comments>http://blog.adampresley.com/2007/my-first-plugin/#comments</comments>
		<pubDate>Tue, 16 Oct 2007 14:57:43 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=126</guid>
		<description><![CDATA[Yes, ladies and gentlemen, I have started learning how to write WordPress plugins. My first plugin is one that removes the word &#8220;Private: &#8221; from the beginning of blog and page titles (when they are saved with a status of private). The way this works is by creating a function that takes one argument. I [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Yes, ladies and gentlemen, I have started learning how to write WordPress plugins. My first plugin is one that removes the word &#8220;Private: &#8221; from the beginning of blog and page titles (when they are saved with a status of private).</p>

<p>The way this works is by creating a function that takes one argument. I then used a regular expression to remove the offending words. The regex I used was <em>/^(private:\s*)/i </em>and then I simply replaced any match with a blank string, removing it from the source text. And in WordPress there are two types of plugins: Action hooks, and Filter hooks.</p>

<p>In this case I used a filter hook, since filter hooks alter text coming or going from the WordPress database. So to make my filter work you have to use the <em>add_filter</em> function to register my function to handle the filter hook &#8220;<em>the_title</em>&#8220;. This hook alters the title of posts and pages before they go out to the browser. You can find my plugin at <a href="http://blog.adampresley.com/wordpress-mods/hide-private-in-title/">http://blog.adampresley.com/wordpress-mods/hide-private-in-title/</a>. Now, here&#8217;s the code.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
	Plugin Name: Hide &quot;Private&quot; In Title
	Plugin URI: http://blog.adampresley.com/?page_id=124
	Description: Removes the word &quot;private:&quot; from the beginning of blog entry
				 	and page titles.
	Version: 1.0
	Author: Adam Presley
	Author URI: http://www.adampresley.com
&nbsp;
	Copyright 2007  Adam Presley  (email : psykoprogrammer@yahoo.com)
&nbsp;
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
&nbsp;
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
&nbsp;
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//--------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">// Name: filter_hidePrivateInTitle</span>
<span style="color: #666666; font-style: italic;">// Auth: Adam Presley</span>
<span style="color: #666666; font-style: italic;">// Desc: Removes the word &quot;private:&quot; from blog and page titles.</span>
<span style="color: #666666; font-style: italic;">//--------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> filter_hidePrivateInTitle<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(private:\s*)/i'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//---------------------------</span>
<span style="color: #666666; font-style: italic;">// Add the hook to WordPress.</span>
<span style="color: #666666; font-style: italic;">//---------------------------</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_title'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'filter_hidePrivateInTitle'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>



<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2007/my-first-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caching in PHP: Cache_Lite</title>
		<link>http://blog.adampresley.com/2007/caching-in-php-cache_lite/</link>
		<comments>http://blog.adampresley.com/2007/caching-in-php-cache_lite/#comments</comments>
		<pubDate>Wed, 30 May 2007 22:28:25 +0000</pubDate>
		<dc:creator>Adam Presley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.adampresley.com/?p=67</guid>
		<description><![CDATA[For many web application developers the concept of caching is foreign. Most of us build our pages, query our databases, and spit out the results. This is usually sufficient as the content is entirely dynamic. There are times, however, when the data, although not static, doesn&#8217;t change very frequently, and can benefit from caching. Caching [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>For many web application developers the concept of caching is foreign. Most of us build our pages, query our databases, and spit out the results. This is usually sufficient as the content is entirely dynamic. There are times, however, when the data, although not static, doesn&#8217;t change very frequently, and can benefit from caching.</p>

<p>Caching can come in many complex flavors, but in this blog I will talk about a <strong>very simple</strong> caching mechanism accomplished by using the PEAR library&#8217;s Cache_Lite class. Cache_Lite allows you to cache some specific piece of data, or page content, or whatever, and retrieve it quickly for reuse.</p>

<p>As an example let us say you have a database table with all of your customers and information about them. This table is not likely to change <strong>TOO</strong> quickly, so perhaps a web page that allows you to search and manage these customers would benefit from caching. Let us also say that this table is named <em>customers</em>, and you have one on 2 different database servers, because you have several clusters. Lets also assume you have an XML file that is the <strong>master</strong> list of all customers across all clusters.</p>

<p>If you are building a web page that can search for customer by providing information in a search criteria, you would have to read the XML file, and then go to the customer table for each cluster to find the information you want. Well, perhaps there is a better way. In this example I am using an imaginary XML file which would look like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customerName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>customerA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customerName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>DB100<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;active<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/active<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<p>With this information we can then go the customer table on the database server that each customer is on to retrieve additional information.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache/Lite.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$cacheId</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UniqueCacheId&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cacheXmlGroup</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xmlGroup&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cacheCustomerGroup</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;customerGroup&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$cacheOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
   <span style="color: #0000ff;">&quot;lifeTime&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">,</span>
   <span style="color: #0000ff;">&quot;automaticSerialization&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cache_Lite<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheOptions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//--------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">// Read the XML master customer file. If the data is already in the</span>
<span style="color: #666666; font-style: italic;">// cache use that.</span>
<span style="color: #666666; font-style: italic;">//--------------------------------------------------------------</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheData</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheId</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cacheXmlGroup</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000088;">$xmlCustomerList</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cacheData</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$xmlCustomerList</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;customerXmlFile.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlCustomerList</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cacheId</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cacheXmlGroup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//----------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">// Get the detail information from the database server/cluster this customer is on.</span>
<span style="color: #666666; font-style: italic;">// Assume we are already connected to each cluster database, and get all the</span>
<span style="color: #666666; font-style: italic;">// data. We'll pretend we have an array of mysql connections. If this data</span>
<span style="color: #666666; font-style: italic;">// is already cached, use that.</span>
<span style="color: #666666; font-style: italic;">//----------------------------------------------------------------------------</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheData</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheId</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cacheCustomerGroup</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000088;">$customerArray</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cacheData</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$connectionArray</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$qry</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM customers&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$customerArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customerName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;additionalInfo1&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">additionalInfo1</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$customerArray</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customerName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;additionalInfo2&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">additionalInfo2</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$customerArray</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cacheId</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cacheCustomerGroup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>In this example we are loading two pieces of data. The first is an XML file with a master customer list, containing all customers across multiple clusters. Each cluster has a database server with a customer table that has detailed information about customer on IT&#8217;S cluster. With this data retrieved you can then do just about anything with that data. Search on it&#8230; Manipulate it&#8230; Data mining&#8230; whatever.</p>

<p>The COOL thing about this is the caching. While the cache file is still valid the XML and customer information data is pulled from the cache file instead of reading the XML and querying the database EACH time. This is a significant speed boost.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.adampresley.com/2007/caching-in-php-cache_lite/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
