title: Applying array_map recursively (array_walk_recursive?)
body: <p>I have an associative array that is I am creating from an ODBC query with the following code:</p>
  
  <pre><code>        while ($row=odbc_fetch_array($oexec)) {
          if(empty($group[$row['gmm']])) {
              $group[$row['gmm']] = array();
          }
          if(empty($group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']])) {
              $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']] = array();
          }
          if(empty($group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']])) {
              $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']] = array();
          }
          if(empty($group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']])) {
              $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']] = array();
          }
          if(empty($group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']])) {
              $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']] = array();
          }
          $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']]['total_ty_yest_sales'] = $row['total_ty_yest_sales'];
          $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']]['total_wo_dotcom_ty_yest_sales'] = $row['total_wo_dotcom_ty_yest_sales'];
          $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']]['east_ty_yest_sales'] = $row['east_ty_yest_sales'];
          $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']]['central_ty_yest_sales'] = $row['central_ty_yest_sales'];
          $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']]['west_ty_yest_sales'] = $row['west_ty_yest_sales'];
          $group[$row['gmm']][$row['acctg_dept_nbr'] . " - " . $row['acctg_dept_desc']][$row['dept_catg_grp_desc']][$row['dept_category_desc']][$row['dept_subcatg_desc']]['dotcom_ty_yest_sales'] = $row['dotcom_ty_yest_sales'];
  
      }
  </code></pre>
  
  <p>This gives me an array where:</p>
  
  <pre><code>$myArray = Array(GMM =&gt; Array(acctg_dept_nbr =&gt; Array(dept_category_desc =&gt; Array(dept_subcatg_desc =&gt; Array(value1,value2,value3)))))
  </code></pre>
  
  <p>I want to sum the values at every level. So for every acctg_dept_nbr[dept_category_desc][dept_subcatg_desc] I want to sum value1,value2,value3. Same for the GMM level and down to the dept_subcatg_desc level. Summing the dept_subcatg_desc level wasn't a problem. I dug around and found how to sum the dept_category_desc level, but am having trouble applying that method recursively.</p>
  
  <p>Here is the code that puts the values into a table:</p>
  
  <pre><code>foreach($group as $gmm =&gt; $acctg_dept_nbrs) {
          echo "&lt;tr class=\"header\"&gt;
          &lt;td&gt;" . $gmm . "&lt;/td&gt;\n";
  
          foreach ($acctg_dept_nbrs as $acctg_dept_nbr =&gt; $dept_catg_grp_descs) {
                          echo "&lt;tr class=\"header\"&gt;\n
              &lt;td style=\"padding-left: 1em;\"&gt;" . $acctg_dept_nbr . "&lt;/td&gt;\n";
  
              foreach($dept_catg_grp_descs as $dept_catg_grp_desc =&gt; $dept_category_descs) {
                  echo "&lt;tr class=\"header\"&gt;\n
                  &lt;td style=\"padding-left: 2em;\"&gt;" . $dept_catg_grp_desc . "&lt;/td&gt;\n";
                                      //echo "&lt;td&gt;" . array_sum(array_walk_recursive($dept_category_descs,function($item) {return $item['total_ty_yest_sales'];})) . "&lt;/td&gt;";
                  foreach($dept_category_descs as $dept_category_desc =&gt; $dept_subcatg_descs) {
                      echo "&lt;tr class=\"header\"&gt;\n
                      &lt;td style=\"padding-left: 3em;\"&gt;" . $dept_category_desc . "&lt;/td&gt;\n";
                                              echo "&lt;td&gt;" . array_sum(array_map(function($item) {return $item['total_ty_yest_sales'];},$dept_subcatg_descs)) . "&lt;/td&gt;";
                                              echo "&lt;td&gt;" . array_sum(array_map(function($item) {return $item['east_ty_yest_sales'];},$dept_subcatg_descs)) . "&lt;/td&gt;";
                                              echo "&lt;td&gt;" . array_sum(array_map(function($item) {return $item['central_ty_yest_sales'];},$dept_subcatg_descs)) . "&lt;/td&gt;";
                                              echo "&lt;td&gt;" . array_sum(array_map(function($item) {return $item['west_ty_yest_sales'];},$dept_subcatg_descs)) . "&lt;/td&gt;";
                      foreach($dept_subcatg_descs as $dept_subcatg_desc =&gt; $values) {
                                                  echo "&lt;tr&gt;\n
                                                      &lt;td style=\"padding-left: 4em;\"&gt;" . $dept_subcatg_desc . "&lt;/td&gt;\n";
                                                  $sum = $values['total_ty_yest_sales'];
                                                  echo "&lt;td&gt;".$sum."&lt;/td&gt;";
                                                  $sum = $values['east_ty_yest_sales'];
                                                  echo "&lt;td&gt;".$sum."&lt;/td&gt;";
                                                  $sum = $values['central_ty_yest_sales'];
                                                  echo "&lt;td&gt;".$sum."&lt;/td&gt;";
                                                  $sum = $values['west_ty_yest_sales'];
                                                  echo "&lt;td&gt;".$sum."&lt;/td&gt;";
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>The commented out line is the problem for me now. This:</p>
  
  <pre><code>array_sum(array_map(function($item) {return $item['west_ty_yest_sales'];},$dept_subcatg_descs))
  </code></pre>
  
  <p>works fine at that level, but not at the higher levels. I have also tried to tweak this function to no avail:</p>
  
  <pre><code>function array_map_recursive($callback, $array) {
      foreach ($array as $key =&gt; $value) {
          if (is_array($array[$key])) {
              $array[$key] = array_map_recursive($callback, $array[$key]);
          }
          else {
              $array[$key] = call_user_func($callback, $array[$key]);
          }
      }
      return $array;
  }
  </code></pre>
  
  <p>How can I make this work so that regardless of level it will dig down and sum the values for that portion of the array?</p>