title: How can I compare two arrays and list differences in PHP?
body: <p>I'm building a form to do the following:</p>
  <ul>
  <li>Print a table of users and permissions, pulling from MySQL. Each permission a user has is a checked box, and each one they lack is an unchecked box.</li>
  <li>Allow an administrator to check and uncheck boxes to grant or remove permissions.</li>
  <li>When the form is submitted, show a confirmation page with ONLY the users whose permissions will be changed, highlighting the specific changes.</li>
  <li>When the changes are confirmed, modify the database accordingly.</li>
  </ul>
  <p>To do this, I'm creating two arrays of user permissions: one according to what the database shows, and one according to what the form shows.</p>
  <p>If a user lacks a permission in MySQL, it will be shown as a 0. If they lack a permission in the form submission, it simply won't exist.</p>
  <p>Here are two simple examples of the arrays:</p>
  <h3>Database array</h3>
  <pre>
  [User1] => Array ([public] => 1
                  [private] => 1
                  [secret] => 1
                 ) 
  [User2] => Array ([public] => 1
                  [private] => 0
                  [secret] => 0
                 )
  </pre>
  <h3>Form submission array (revoking &quot;secret&quot; from User1 and giving it to User2)</h3>
  <pre>
  [User1] => Array ([public] => 1
                  [private] => 1 
                 )
  
  [User2] => Array ([public] => 1
                    [secret] => 1
                   )
  </pre>
  <h2>Question</h2>
  <p>How can I <b>elegantly combine these two arrays</b> to make a &quot;changes&quot; array, such that:</p>
  <ul>
  <li>Users with identical permissions in both are omitted</li>
  <li>Remaining users have all permissions - public, private and secret - set to either 0 or 1.</li>
  <li>Each permission is 0 if it was missing from the form submission array, and 1 if it was in the form submission array</li>
  </ul>
  <p>For example, combining the above would give:</p>
  <pre>
  [User1] => Array ([public] => 1
                  [private] => 1
                  [secret] => 0
                 ) 
  [User2] => Array ([public] => 1
                  [private] => 0
                  [secret] => 1
                 )
  </pre>
  <h2>Attempts so far</h2>
  <ul>
  <li>As a first step, I have tried using array_merge() with the forms array listed second, thinking it would overwrite the database array where they differed. Instead, it deleted elements that differed.</li>
  <li>I have tried setting up a foreach() statement to compare the two arrays, but it's becoming complicated, and I think there must be a simpler way</li>
  </ul>
  <h1>UPDATE</h1>
  <p>Whew! A new answer drew my attention back to this old question. I got this working, but later I scrapped this crazy code - it was way too complicated to go back and work with. Instead, I wrote a PHP back end script to change one permission at a time, then wrote an AJAX front end to send changes over it. Much simpler code, and the changes are instantaneous for the user. A highlight effect gives instant, on-page feedback about what has changed.</p>