title: php creating count based on two keys in an array
body: <p>I have an array called <code>$dinnerDetails</code> that looks like this</p>
  
  <pre><code>array:5 [▼
    0 =&gt; array:7 [▼
      "request_id" =&gt; "48"
      "dinner_id" =&gt; "36"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-16T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "yes"
      "requestCount" =&gt; 3
    ]
    1 =&gt; array:7 [▼
      "request_id" =&gt; "51"
      "dinner_id" =&gt; "36"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-16T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "no"
      "requestCount" =&gt; 3
    ]
    2 =&gt; array:7 [▼
      "request_id" =&gt; "65"
      "dinner_id" =&gt; "36"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-16T10:00:00"
      "host_id" =&gt; null
      "new_dinner" =&gt; "no"
      "requestCount" =&gt; 3
    ]
    3 =&gt; array:7 [▼
      "request_id" =&gt; "50"
      "dinner_id" =&gt; "35"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-27T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "yes"
      "requestCount" =&gt; 2
    ]
    4 =&gt; &amp; array:7 [▼
      "request_id" =&gt; "52"
      "dinner_id" =&gt; "35"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-27T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "no"
      "requestCount" =&gt; 2
    ]
  ]
  </code></pre>
  
  <p>You will notice that each subarray has <code>dinner_id</code> and <code>host_id</code>, if both these values exist then it means dinner has been confirmed. What i am trying to do is count the number of confirmed dinners. In an array above <code>dinner_id =&gt; 36</code> has been confirmed by 2 people and the same goes for <code>dinner_id</code> => 35,</p>
  
  <p>I would like to add a new <code>key</code> in the sub array lets call it <code>confirmationCount</code> and put the confirmed count against it so that my output looks like this</p>
  
  <pre><code>array:5 [▼
    0 =&gt; array:8 [▼
      "request_id" =&gt; "48"
      "dinner_id" =&gt; "36"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-16T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "yes"
      "requestCount" =&gt; 3
      "confirmationCount" =&gt; 2
    ]
    1 =&gt; array:8 [▼
      "request_id" =&gt; "51"
      "dinner_id" =&gt; "36"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-16T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "no"
      "requestCount" =&gt; 3
      "confirmationCount" =&gt; 2
    ]
    2 =&gt; array:8 [▼
      "request_id" =&gt; "65"
      "dinner_id" =&gt; "36"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-16T10:00:00"
      "host_id" =&gt; null
      "new_dinner" =&gt; "no"
      "requestCount" =&gt; 3
      "confirmationCount" =&gt; 2
    ]
    3 =&gt; array:8 [▼
      "request_id" =&gt; "50"
      "dinner_id" =&gt; "35"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-27T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "yes"
      "requestCount" =&gt; 2
      "confirmationCount" =&gt; 2
    ]
    4 =&gt; &amp; array:8 [▼
      "request_id" =&gt; "52"
      "dinner_id" =&gt; "35"
      "dinner_name" =&gt; "Black Tie"
      "dinner_date" =&gt; "2016-05-27T10:00:00"
      "host_id" =&gt; "77"
      "new_dinner" =&gt; "no"
      "requestCount" =&gt; 2
      "confirmationCount" =&gt; 2
    ]
  ]
  </code></pre>
  
  <p>So to do this I wrote <code>foreach</code> loop twice, the first loop prepares the array and the second loop adds the count i need.</p>
  
  <pre><code>$dinnerDetails = array();
  $lastDinnerDate = '';
  $newDate = '';
  $countDinner = array();
  $countConfirmation = array();
  //Prepare an array of requests so it can be passed back to the controller
  foreach ($invitations as $invitation) {
  
          $countDinner[$invitation['dinner_id']] = isset($countDinner[$invitation['dinner_id']]) ? $countDinner[$invitation['dinner_id']] + 1 : 1;
          $countConfirmation[$invitation['host_id']] = isset($countConfirmation[$invitation['host_id']]) ? $countConfirmation[$invitation['host_id']] + 1 : 1;
  
          //If the date changes then assign yes, this is used on front end to display date on top of requests
          if (strtotime($invitation['dinner_date']) &gt; $lastDinnerDate) {
              $newDate = 'yes';
          } else {
              $newDate = 'no';
          }
  
          $lastDinnerDate = strtotime($invitation['dinner_date']);
  
          $dinnerDetails[] = array(
              'request_id' =&gt; $invitation['request_id'],
              'dinner_id' =&gt; $invitation['dinner_id'],
              'dinner_name' =&gt; $invitation['dinner_name'],
              'dinner_date' =&gt; $invitation['dinner_date'],
              'host_id' =&gt; $invitation['host_id'],
              'new_dinner' =&gt; $newDate,
          );
  
  }
  
  foreach ($dinnerDetails as &amp;$dinnerDetail) {
      $dinnerDetail['requestCount'] = $countDinner[$dinnerDetail['dinner_id']];
      $dinnerDetail['confirmationCount'] = $countConfirmation[$dinnerDetail['host_id']];
  }
  </code></pre>
  
  <p>The problem I am having is in the code below</p>
  
  <pre><code>$countConfirmation[$invitation['host_id']] = isset($countConfirmation[$invitation['host_id']]) ? $countConfirmation[$invitation['host_id']] + 1 : 1;
  </code></pre>
  
  <p>I need to update this code in a such a way that it takes <code>dinner_id</code> into account as well so the result 'confirmationCount' i get is linked with the dinner. Right now it looks for the <code>host_id</code> and returns the result regardless of if the confirmation is linked with the right dinner or not.</p>