<?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; filter</title>
	<atom:link href="http://neverusethisfont.com/blog/tags/filter/feed/" rel="self" type="application/rss+xml" />
	<link>http://neverusethisfont.com/blog</link>
	<description>Aaron Parecki is the co-founder of Geoloqi.com, and specializes in backend systems development.</description>
	<lastBuildDate>Mon, 11 Jul 2011 22:50:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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&#8242;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" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Career and Life Planning&quot;</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">'Author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Michel Ozzi&quot;</span><span style="color: #339933;">,</span>      <span style="color: #0000ff;">'ISBN'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;978-0-07-284262-3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Beginning Algebra&quot;</span><span style="color: #339933;">,</span>         <span style="color: #0000ff;">'Author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Blitzer&quot;</span><span style="color: #339933;">,</span>          <span style="color: #0000ff;">'ISBN'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;978-0-555-03971-7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Primary Flight Briefing&quot;</span><span style="color: #339933;">,</span>   <span style="color: #0000ff;">'Author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Federal Aviation&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ISBN'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;978-1-5602755-7-2&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_GET</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$v</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$data'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return ( stripos($data[&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$k</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;], &quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$v</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;) !== false );'</span><span style="color: #009900;">&#41;</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></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" style="font-family:monospace;"><span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;ISBN&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;555&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>

