title: increment value inside an array of arrays (if key is non-existent, set it to 1)
body: <p><strong>Question has been updated to clarify</strong></p>
  
  <p>For simple arrays, I find it convenient to use <code>$arr[$key]++</code> to either populate a new element or increment an existing element.  For example, counting the number of fruits, <code>$arr['apple']++</code> will create the array element <code>$arr('apple'=&gt;1)</code> the first time "apple" is encountered.  Subsequent iterations will merely increment the value for "apple".  There is no need to add code to check to see if the key "apple" already exists.</p>
  
  <p>I am populating an array of arrays, and want to achieve a similar "one-liner" as in the example above in an element of the nested array.</p>
  
  <p><code>$stats</code> is the array. Each element in <code>$stats</code> is another array with 2 keys ("name" and "count")</p>
  
  <p>I want to be able to push an array into <code>$stats</code> - if the key already exists, merely increment the "count" value.  If it doesn't exist, create a new element array and set the count to 1.  And doing this in one line, just like the example above for a simple array.</p>
  
  <p>In code, this would look something like (but does not work):</p>
  
  <pre><code>$stats[$key] = array('name'=&gt;$name,'count'=&gt;++);
  or 
  $stats[$key] = array('name'=&gt;$name,++);
  </code></pre>
  
  <p>Looking for ideas on how to achieve this without the need to check if the element already exists.</p>
  
  <p>Background:</p>
  
  <p>I am cycling through an array of objects, looking at the "data" element in each one.  Here is a snip from the array:</p>
  
  <pre><code>[1] =&gt; stdClass Object
      (
          [to] =&gt; stdClass Object
              (
                  [data] =&gt; Array
                      (
                          [0] =&gt; stdClass Object
                              (
                                  [name] =&gt; foobar
                                  [id] =&gt; 1234
                              )
  
                      )
  
              )
  </code></pre>
  
  <p>I would like to count the occurrences of "id" and correlate it to "name". ("id" and "name" are unique combinations - ex. name="foobar" will always have an id=1234)</p>
  
  <p>i.e. </p>
  
  <pre><code>id     name   count
  1234  foobar    55
  6789  raboof    99
  </code></pre>
  
  <p>I'm using an array of arrays at the moment, <code>$stats</code>, to capture the information (I am def. open to other implementations.  I looked into <code>array_unique</code> but my original data is deep inside arrays &amp; objects).</p>
  
  <p>The first time I encounter "id" (ex. 1234), I'll create a new array in <code>$stats</code>, and set the count to 1.  For subsequent hits (ex: id=1234), I just want to increment <code>count</code>.  </p>
  
  <p>For one dimensional arrays, <code>$arr[$obj-&gt;id]++</code> works fine, but I can't figure out how to push/increment for array of arrays.  How can I push/increment in one line for multi-dimensional arrays?  </p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>$stats = array();
  foreach ($dataArray as $element) {
    $obj = $element-&gt;to-&gt;data[0];
    // this next line does not meet my needs, it's just to demonstrate the structure of the array
    $stats[$obj-&gt;id] = array('name'=&gt;$obj-&gt;name,'count'=&gt;1);
  
    // this next line obviously does not work, it's what I need to get working
    $stats[$obj-&gt;id] = array('name'=&gt;$obj-&gt;name,'count'=&gt;++);
  }
  </code></pre>