我有一个关联数组,该数组是我通过使用以下代码的ODBC查询创建的:

        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'];

    }

这给了我一个数组,其中:
$myArray = Array(GMM => Array(acctg_dept_nbr => Array(dept_category_desc => Array(dept_subcatg_desc => Array(value1,value2,value3)))))

我想对每个级别的值求和。因此,对于每个acctg_dept_nbr [dept_category_desc] [dept_subcatg_desc],我想对value1,value2,value3求和。对于GMM级别相同,直到dept_subcatg_desc级别。总结dept_subcatg_desc的级别不是问题。我仔细研究了一下,发现了如何对dept_category_desc级别求和,但是在递归地应用该方法时遇到了麻烦。

这是将值放入表中的代码:
foreach($group as $gmm => $acctg_dept_nbrs) {
        echo "<tr class=\"header\">
        <td>" . $gmm . "</td>\n";

        foreach ($acctg_dept_nbrs as $acctg_dept_nbr => $dept_catg_grp_descs) {
                        echo "<tr class=\"header\">\n
            <td style=\"padding-left: 1em;\">" . $acctg_dept_nbr . "</td>\n";

            foreach($dept_catg_grp_descs as $dept_catg_grp_desc => $dept_category_descs) {
                echo "<tr class=\"header\">\n
                <td style=\"padding-left: 2em;\">" . $dept_catg_grp_desc . "</td>\n";
                                    //echo "<td>" . array_sum(array_walk_recursive($dept_category_descs,function($item) {return $item['total_ty_yest_sales'];})) . "</td>";
                foreach($dept_category_descs as $dept_category_desc => $dept_subcatg_descs) {
                    echo "<tr class=\"header\">\n
                    <td style=\"padding-left: 3em;\">" . $dept_category_desc . "</td>\n";
                                            echo "<td>" . array_sum(array_map(function($item) {return $item['total_ty_yest_sales'];},$dept_subcatg_descs)) . "</td>";
                                            echo "<td>" . array_sum(array_map(function($item) {return $item['east_ty_yest_sales'];},$dept_subcatg_descs)) . "</td>";
                                            echo "<td>" . array_sum(array_map(function($item) {return $item['central_ty_yest_sales'];},$dept_subcatg_descs)) . "</td>";
                                            echo "<td>" . array_sum(array_map(function($item) {return $item['west_ty_yest_sales'];},$dept_subcatg_descs)) . "</td>";
                    foreach($dept_subcatg_descs as $dept_subcatg_desc => $values) {
                                                echo "<tr>\n
                                                    <td style=\"padding-left: 4em;\">" . $dept_subcatg_desc . "</td>\n";
                                                $sum = $values['total_ty_yest_sales'];
                                                echo "<td>".$sum."</td>";
                                                $sum = $values['east_ty_yest_sales'];
                                                echo "<td>".$sum."</td>";
                                                $sum = $values['central_ty_yest_sales'];
                                                echo "<td>".$sum."</td>";
                                                $sum = $values['west_ty_yest_sales'];
                                                echo "<td>".$sum."</td>";
                    }
                }
            }
        }
    }

现在,注释掉的行对我来说是个问题。这:
array_sum(array_map(function($item) {return $item['west_ty_yest_sales'];},$dept_subcatg_descs))

在该级别上可以正常工作,但在更高级别上则不能。我也试图调整此功能无济于事:
function array_map_recursive($callback, $array) {
    foreach ($array as $key => $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;
}

我该如何进行这项工作,以便无论其级别如何,都将对其进行挖掘并求和该数组那部分的值?

最佳答案

function array_map_recursive($callback, $array)
{
  $func = function ($item) use (&$func, &$callback) {
    return is_array($item) ? array_map($func, $item) : call_user_func($callback, $item);
  };

  return array_map($func, $array);
}

关于php - 递归地应用array_map(array_walk_recursive?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22204254/

10-11 06:06