title: How to get the sum of salaries from a multidimentional array after condition
body: <p>In this project i have a multidimentional array that keeps some employees names, their position, their speciality and their salary. I need to find the average salary of all the managers and all the employees from the array.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta sharset="UTF-8"/&gt;
  &lt;title&gt;Project3&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;?php
  $employees = array(array("name" =&gt; "Nikolaou Nikolaos",
                           "occupation" =&gt; "Employee",
                           "salary" =&gt; 1500,
                           "specialty" =&gt; "Web Programmer"),
                     array("name" =&gt; "Papadopoulou Anna",
                           "occupation" =&gt; "Manager",
                           "salary" =&gt; 2300,
                           "specialty" =&gt; "Human resources management"),
                     array("name" =&gt; "Alexiou Nikoleta",
                           "occupation" =&gt; "Employee",
                           "salary" =&gt; 800,
                           "specialty" =&gt; "Secretary"),
                  );
  
  //Start of the function that prints the arrays in a specific way.
  function printTable($table) 
  {   
      foreach ($table as $employee =&gt; $list) {
  
          // Print a heading:
          echo "&lt;h2 style=\"text-align:left; font-size:26px; font-family:times new roman; color:blue\"&gt;Employee #$employee&lt;/h2&gt;&lt;ul&gt;";
  
          // Print each district data:
          foreach ($list as $key =&gt; $value) {
              echo "&lt;li style=\"text-align:left; font-size:18px; font-family:times new roman; color:black\"&gt;$key: $value&lt;/li&gt;\n"; 
          }// End of nested FOREACH.
  
          // Close the list:
          echo '&lt;/ul&gt;';
  
      } // End of main FOREACH.
  }// End of the function.
  
  //function that calculates and returns the average salary of employees and managers separately.
  function calcMeanAges($table, &amp;$hmean, &amp;$gmean) 
  {
      $cEmployees = 0;
      $sumsalary_e = 0;
      $cManagers = 0;
      $sumsalary_m = 0;
      foreach ($table as $occupation =&gt;$list) {
          foreach ($list as $salary =&gt;$value) {
              if ($occupation == "Employee") {
                  $cEmployees++;
                  $sumsalary_e += $salary['salary'];
              }
              if ($occupation == "Manager") {
                  $cManagers++;
                  $sumsalary_m += $salary['salary'];
              }
          }
      }
      $hmean = $sumsalary_e / $cEmployees;
      $gmean = $sumsalary_m / $cManagers;
  }
  
  //call of the function that calculates the average salaries.
  calcMeanAges($employees, $h, $g); 
  echo "$h&lt;br&gt;";
  echo "$g";
  
  //Printing the elements of the arrays
  echo '&lt;p style="color:red;font-size:28px;font-family:times new roman;"&gt;&lt;b&gt;Employees and managers&lt;/b&gt;&lt;/p&gt;&lt;br&gt;';
  
  printTable($employees);
  ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I need to calculate the average salaries of employees and managers separately. So i thought of putting a condition in the <code>Foreach()</code> to check who is who and then count them and keep the sum of their salary. But it doesn't work properly. I would appreciate some help.</p>