<?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>Andrew Havens Blog &#187; arrays</title>
	<atom:link href="http://blog.andrewhavens.com/tag/arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.andrewhavens.com</link>
	<description></description>
	<lastBuildDate>Mon, 14 Dec 2009 19:41:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to compare the differences between two arrays in PHP</title>
		<link>http://blog.andrewhavens.com/2008/10/30/how-to-compare-the-differences-between-two-arrays-in-php/</link>
		<comments>http://blog.andrewhavens.com/2008/10/30/how-to-compare-the-differences-between-two-arrays-in-php/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 19:21:40 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.amperactive.com/?p=27</guid>
		<description><![CDATA[ Question: How do I write a function to compare the differences between two arrays?
Consider the following scenarios:
$alreadyInTheDatabase = array('name' =&#62; 'Joe', 'address' =&#62; '123 Fake St');
$newFormValuesArray = array('name' =&#62; 'Joe', 'address' =&#62; 'My address has changed');

$alreadyInTheDatabase = array('name' =&#62; 'Joe', 'address' =&#62; '123 Fake St');
$newFormValuesArray = array('name' =&#62; 'Joe'); //a bad form has address [...]]]></description>
			<content:encoded><![CDATA[<!-- Generated by Digg Digg plugin, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/
	--><div style='float:right'><table > <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http%3A%2F%2Fblog.andrewhavens.com%2F2008%2F10%2F30%2Fhow-to-compare-the-differences-between-two-arrays-in-php%2F&amp;t=How+to+compare+the+differences+between+two+arrays+in+PHP&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td></table></div><p><strong>Question:</strong> How do I write a function to compare the differences between two arrays?</p>
<p>Consider the following scenarios:</p>
<pre class="brush: php;">$alreadyInTheDatabase = array('name' =&gt; 'Joe', 'address' =&gt; '123 Fake St');
$newFormValuesArray = array('name' =&gt; 'Joe', 'address' =&gt; 'My address has changed');

$alreadyInTheDatabase = array('name' =&gt; 'Joe', 'address' =&gt; '123 Fake St');
$newFormValuesArray = array('name' =&gt; 'Joe'); //a bad form has address missing from the array

$alreadyInTheDatabase = array('name' =&gt; 'Joe'); //address was never in the database to begin with
$newFormValuesArray = array('name' =&gt; 'Joe', 'address' =&gt; '123 Fake St');</pre>
<p>I&#8217;m writing a function to check whether or not I need to update a field in a database, so my function will tell me what the differences are between what&#8217;s already in the database, and what form fields have changed. If the field is the same as the one that&#8217;s in the database, I won&#8217;t need to update it. If the values are different, I&#8217;ll update. If the form has a field that doesn&#8217;t even exist yet in the database (because it hasn&#8217;t been created yet), I&#8217;ll need to know that too. So I&#8217;ll just return an array of the keys that differ.</p>
<p>There are a lot of PHP functions that exist to solve all sorts of scenarios where you would want to compare arrays, but none of them return an array of the differences between <em>both</em> arrays.</p>
<blockquote><p><a href="http://us3.php.net/manual/en/function.array-diff.php">array_diff</a> — Computes the difference of arrays<br />
<a href="http://us3.php.net/manual/en/function.array-udiff.php">array_udiff</a> — Computes the difference of arrays by using a callback function for data comparison<br />
<a href="http://us3.php.net/manual/en/function.array-diff-key.php">array_diff_key</a> — Computes the difference of arrays using keys for comparison<br />
<a href="http://us3.php.net/manual/en/function.array-diff-uassoc.php">array_diff_uassoc</a> — Computes the difference of arrays with additional index check which is performed by a user supplied callback function<br />
<a href="http://us3.php.net/manual/en/function.array-diff-ukey.php">array_diff_ukey</a> — Computes the difference of arrays using a callback function on the keys for comparison<br />
<a href="http://us3.php.net/manual/en/function.array-intersect.php">array_intersect</a> — Computes the intersection of arrays<br />
<a href="http://us3.php.net/manual/en/function.array-uintersect.php">array_uintersect</a> — Computes the intersection of arrays, compares data by a callback function<br />
<a href="http://us3.php.net/manual/en/function.array-intersect-assoc.php">array_intersect_assoc</a> — Computes the intersection of arrays with additional index check<br />
<a href="http://us3.php.net/manual/en/function.array-intersect-key.php">array_intersect_key</a> — Computes the intersection of arrays using keys for comparison<br />
<a href="http://us3.php.net/manual/en/function.array-intersect-uassoc.php">array_intersect_uassoc</a> — Computes the intersection of arrays with additional index check, compares indexes by a callback function<br />
<a href="http://us3.php.net/manual/en/function.array-intersect-ukey.php">array_intersect_ukey</a> — Computes the intersection of arrays using a callback function on the keys for comparison<br />
<a href="http://us3.php.net/manual/en/function.array-udiff-assoc.php">array_udiff_assoc</a> — Computes the difference of arrays with additional index check, compares data by a callback function<br />
<a href="http://us3.php.net/manual/en/function.array-udiff-uassoc.php">array_udiff_uassoc</a> — Computes the difference of arrays with additional index check, compares data and indexes by a callback function<br />
<a href="http://us3.php.net/manual/en/function.array-uintersect-assoc.php">array_uintersect_assoc</a> — Computes the intersection of arrays with additional index check, compares data by a callback function<br />
<a href="http://us3.php.net/manual/en/function.array-uintersect-uassoc.php">array_uintersect_uassoc</a> — Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions</p></blockquote>
<p>Quite a lengthy list, isn&#8217;t it?? Now you can sense my confusion and frustration. We want to check the values, but return the key of the value that is different. We&#8217;ll also check the keys to see if we need to create a field that doesn&#8217;t already exist in the database.</p>
<p><strong>Answer:</strong> We&#8217;ll use array_diff_assoc() to return an associative array of the keys and values that differ in the first array. Then we&#8217;ll swap the arrays to check the second array against the first. We&#8217;ll merge the two arrays of differences using array_merge() and return just the keys by using array_keys().</p>
<pre class="brush: php;">function getDifferences($alreadyInTheDatabase, $newFormValuesArray) {

$diff1 = array_diff_assoc($alreadyInTheDatabase, $newFormValuesArray);
$diff2 = array_diff_assoc($newFormValuesArray, $alreadyInTheDatabase);

$merged = array_merge($diff1, $diff2);

$justTheKeys = array_keys($merged);

return $justTheKeys;

}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.andrewhavens.com/2008/10/30/how-to-compare-the-differences-between-two-arrays-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
