<?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>Never Use This Font &#187; search</title>
	<atom:link href="http://neverusethisfont.com/blog/tags/search/feed/" rel="self" type="application/rss+xml" />
	<link>http://neverusethisfont.com/blog</link>
	<description>Ramblings of a Designer/Programmer</description>
	<lastBuildDate>Fri, 09 Jul 2010 21:50:35 +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>Filtering Array Elements in PHP Using an Anonymous Function</title>
		<link>http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/</link>
		<comments>http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 18:35:01 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[Website Development]]></category>
		<category><![CDATA[anonymous functions]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/</guid>
		<description><![CDATA[Using an anonymous function in PHP's array_filter as a way to search a 2D array.]]></description>
			<content:encoded><![CDATA[<div style="text-align:center; background-color:#eee; border: 1px #bbb solid; padding: 4px;">== UPDATE ==<br />
This is now basically obsolete thanks to PHP 5.3&#8217;s <a href="http://www.php.net/manual/en/functions.anonymous.php">closures</a></div>
<p>I&#8217;ve used PHP&#8217;s <a href="http://php.net/array_filter">array_filter()</a> in the past to filter elements from an array, but occasionally I find that I need to pass an additional value to the function in order to determine whether to keep the element. This just came up in a case where I was using GET variables to filter search results on a large array.</p>
<p>I have a large 2D array of books where each record has some information such as title, author, publisher, ISBN, etc. I include search boxes for each column when I draw the table to the screen, so the user can search a specific column for some text.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #ff0000">$data</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span>
    <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Title'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Career and Life Planning&quot;</span><span style="color: #66cc66;">,</span>  <span style="color: #ff0000;">'Author'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Michel Ozzi&quot;</span><span style="color: #66cc66;">,</span>      <span style="color: #ff0000;">'ISBN'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;978-0-07-284262-3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Title'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Beginning Algebra&quot;</span><span style="color: #66cc66;">,</span>         <span style="color: #ff0000;">'Author'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Blitzer&quot;</span><span style="color: #66cc66;">,</span>          <span style="color: #ff0000;">'ISBN'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;978-0-555-03971-7&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Title'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Primary Flight Briefing&quot;</span><span style="color: #66cc66;">,</span>   <span style="color: #ff0000;">'Author'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Federal Aviation&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ISBN'</span> <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;978-1-5602755-7-2&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000">$_GET</span> <span style="color: #b1b100;">as</span> <span style="color: #ff0000">$k</span><span style="color: #66cc66;">=&gt;</span><span style="color: #ff0000">$v</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #ff0000">$data</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array_filter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$data</span><span style="color: #66cc66;">,</span> <span style="color: #000066;">create_function</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'$data'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'return ( stripos($data[&quot;'</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$k</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">'&quot;], &quot;'</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$v</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">'&quot;) !== false );'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Notice the values of <code lang="php">$k</code> and <code lang="php">$v</code> are actually written into the PHP code, whereas <code lang="php">$data</code> is written to the function as a variable name. If you were to echo the body of the function, it would actually look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> stripos<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;ISBN&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;555&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">!==</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>In each iteration of the loop the function is re-created, and the key/value pair are essentially hard-coded into the function instead of being variables.</p>
<p>So there you go! A real-live use for anonymous functions in PHP!</p>
]]></content:encoded>
			<wfw:commentRss>http://neverusethisfont.com/blog/2009/02/filtering-array-elements-in-php-using-an-anonymous-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
