title: Comparing nested multidimensional arrays
body: <p>I am trying to compare nested multidimensional arrays by value and key so my scenario is:</p>
  
  <p>I need to compare two nested multidimensional arrays and find the difference in terms of their values by quantity and update those quantities but also items which are in the new array and not the old one for example:</p>
  
  <p>if I have the following data:</p>
  
  <p><strong>First Array:</strong></p>
  
  <pre><code>Array (
      [0] =&gt; Array ( [name] =&gt; hey [qty] =&gt; 3 )  
      [1] =&gt; Array ( [name] =&gt; hello [qty] =&gt; 1 )  
      [2] =&gt; Array ( [name] =&gt; test [qty] =&gt; 1 )
  )
  </code></pre>
  
  <p>And another nested multidimensional array with the following values:</p>
  
  <p><strong>Second Array:</strong> </p>
  
  <pre><code>Array (
      [0] =&gt; Array ( [name] =&gt; hey [qty] =&gt; 5 )  
      [1] =&gt; Array ( [name] =&gt; hello [qty] =&gt; 5 )  
      [2] =&gt; Array ( [name] =&gt; PHP [qty] =&gt; 2 )
  )
  </code></pre>
  
  <p>I am trying to achieve the following output with only the new items by key in second array and update their values based upon the difference between the first and second array item's quantities i.e.:</p>
  
  <p><strong>Desired output</strong></p>
  
  <pre><code>Array (
      [0] =&gt; Array ( [name] =&gt; hey [qty] =&gt; 2 )  
      [1] =&gt; Array ( [name] =&gt; hello [qty] =&gt; 4 )  
      [2] =&gt; Array ( [name] =&gt; PHP [qty] =&gt; 2 )
  )
  </code></pre>
  
  <p>I am producing the difference using the following but note how PHP is not added. I am not too sure how to check for it in my inner for loop without adding it. </p>
  
  <pre><code>&lt;?php
  
  $items = [
      'hey',
      'hey',
      'hey',
      'hello',
      'test'
  ];
  
  $arr = array_count_values($items);
  
  
  
  $oldItems = array();
  foreach ($arr as $name =&gt; $qty) {
      $oldItems[] = compact('name', 'qty');
  }
  
  
  
  $newItems = [
      ['name' =&gt; 'hey', 'qty' =&gt; 5],
      ['name' =&gt; 'hello', 'qty'=&gt; 5],
      ['name' =&gt; 'PHP', 'qty' =&gt; 2]
  ];
  
  
  $diff = [];
  
  foreach($newItems as $newItem) {
      foreach($oldItems as $oldItem) {
          if ($newItem['name'] == $oldItem['name']) {
          //get quantity
              $qtyDiff = $newItem['qty'] - $oldItem['qty'];
            if ($qtyDiff &gt; 0) {
               $diff[$newItem['name']] = $qtyDiff;
            }
          }
      }
  }
  
  print_r($diff); die();
  </code></pre>
  
  <p>My <strong>current output from this script</strong> is as follows:</p>
  
  <pre><code>Array (
      [0] =&gt; Array ( [name] =&gt; hey [qty] =&gt; 2 )  
      [1] =&gt; Array ( [name] =&gt; hello [qty] =&gt; 4 )
  )  
  </code></pre>
  
  <p>Any help is appreciated or feedback on improvements. Thanks!</p>