How to compare array a with b and create new arrays c (items in a and b), d (items in a, not in b), and e (items in b, not in a)?
<p>Consider the following arrays. They represent the top-5 performing employees for <code>yesterday</code> and <code>today</code>:</p>

<pre><code>$yesterday = array(
   6 =&gt; array('name' =&gt; 'Tod', 'score' =&gt; 9.5),
  12 =&gt; array('name' =&gt; 'Jim', 'score' =&gt; 7.3),
  18 =&gt; array('name' =&gt; 'Bob', 'score' =&gt; 8.4),
   7 =&gt; array('name' =&gt; 'Jan', 'score' =&gt; 6.2),
  20 =&gt; array('name' =&gt; 'Sam', 'score' =&gt; 6.0),
);

$today = array(
   6 =&gt; array('name' =&gt; 'Tod', 'score' =&gt; 9.1),
   9 =&gt; array('name' =&gt; 'Jef', 'score' =&gt; 9.3),
  35 =&gt; array('name' =&gt; 'Axl', 'score' =&gt; 7.6),
   7 =&gt; array('name' =&gt; 'Jan', 'score' =&gt; 6.5),
  41 =&gt; array('name' =&gt; 'Ted', 'score' =&gt; 8.0),
);
</code></pre>

<p>I need 3 new arrays compiled from the above: <code>$stay</code> holding employees who were in the top-5 yesterday, and still are today, <code>$gone</code>, holding employees who were in the top-5 yesterday, but are not anymore, and <code>$new</code>, holding newcomers to <code>$today</code>s top-5 list:</p>

<pre><code>// notice that the scores in $stay come from $today, not $yesterday
// also notice that index keys are maintained

$stay = array(
  6 =&gt; array('name' =&gt; 'Tod', 'score' =&gt; 9.1),
  7 =&gt; array('name' =&gt; 'Jan', 'score' =&gt; 6.5)

);

$gone = array(
  12 =&gt; array('name' =&gt; 'Jim', 'score' =&gt; 7.3),
  18 =&gt; array('name' =&gt; 'Bob', 'score' =&gt; 8.4),
  20 =&gt; array('name' =&gt; 'Sam', 'score' =&gt; 6.0)
);

$new = array(
   9 =&gt; array('name' =&gt; 'Jef', 'score' =&gt; 9.3),
  35 =&gt; array('name' =&gt; 'Axl', 'score' =&gt; 7.6),
  41 =&gt; array('name' =&gt; 'Ted', 'score' =&gt; 8.0)
);
</code></pre>

<p>I have no clue on how to build the logic here. I started with a loop, but didn't get far. I believe it should be something like this. Could you help me get this right?</p>

<pre><code>for ($i = 0; $i &lt; count($yesterday); $i++) {
  // I'm comparing key numbers, but not key values
  // how do I compare key values?
  if (in_array($yesterday[$i], $today) {
    // add to $stay array
  }
  else {
    // add to $gone array
  }
}

for ($i = 0; $i &lt; count($today); $i++) {
  if (!in_array($today[$i], $yesterday) {
    // add to $new array
  }
}
</code></pre>

<p>P.S. I don't know if this is helpfull, but <code>$yesterday</code> and <code>$today</code> are always of equal length (in this case, 5 items, but other scenarios are possible in which both arrays hold 7 or 10 items for instance). The combined items of <code>$stay</code> and <code>$new</code> logically are always equal to the number of items in either <code>$yesterday</code> or <code>$today</code> :-)</p>
