繁体   English   中英

从PHP数组递归计算值的方法?

[英]Method for recursively calculating values from a PHP array?

这个问题在某种程度上与层次结构数据有关,但是我已经从数据库中获得了一个存储过程,该存储过程将返回下面提到的数据。 我没有试图找出如何构建层次结构(完成),而是试图找到使用PHP(也许是递归数组)基于该层次结构计算数字的最佳方法。

通过使用自定义存储过程,我可以拉出我们公司的销售代表,他们销售的产品的列表,以及这些产品在新行上及其后续销售代表(如果在树中)的“获利”百分比。 。

返回的数据的示例如下:

Repnum | Name | productID | sales | earn | depth | parentRep
------------------------------------------------------------
1      |   A  |   1       | 5000  | 0.50 |  1    | 0
1      |   A  |   2       | 10000 | 0.35 |  1    | 0  
2      |   B  |   1       | 400   | 0.40 |  2    | 1
2      |   B  |   2       | 1000  | 0.30 |  2    | 1
7      |   E  |   1       | 30000 | 0.35 |  3    | 2
3      |   C  |   1       | 5000  | 0.33 |  2    | 1

可以安全地假设返回的数据的顺序已经根据此处未提供的深度和信息进行了适当的格式化和排序。 在树形视图中,上面的数据如下所示:

1-A-1-5000-0.50-1-0
1-A-2-10000-0.35-1-0
   2-B-1-400-0.40-2-1
   2-B-2-1000-0.30-2-1
      7-E-1-30000-0.35-3-2
   3-C-1-5000-0.33-2-1

在我的while(result)循环中,我试图为返回的每个repnum计算以下内容:

  • 该代表人数的销售总额乘以每种产品的收入百分比(其总销售额和收入金额)
  • 每个下线树的总销售额乘以当前代表及其下一层的差异

为了说明如何工作:

Rep 1收入:

Rep 1 total sales = 5000 + 10000 = 15,000
Rep 1 earn on self = (5000 * 0.50) + (10000 * 0.35) = 6,000 hello!

Rep 2 total sales = 400 + 1000 = 1400
Rep 1 earn on rep 2 sales = (400 * (0.50-0.40) + (1000 * (.35-0.30)) = 90

Rep 7 total sales = 30000
Rep 1 earn on rep 7 sales = (30000 * (0.50-0.40)) = 3000*
(* the reason the earn is calculated at rep 1 earn - rep 2 earn is because in any tree, the "parent" can only ever earn the difference of his/her earn and the direct depth below him/her)

Rep 3 total sales = 5000
Rep 1 earn on rep 3 sales = (5000 * (0.50-0.33)) = 850

**Rep 1 TOTAL earn = 6,000 + 90 + 3000 + 850 = 9,940**

代表2收入:

Rep 2 total sales = 400 + 1000 = 1400
Rep 2 total earn on self = (400 * 0.40) + (1000 * 0.30) = 460

Rep 7 total sales = 30000
Rep 2 total earn on rep 7 sales = (30000 * (0.40-0.35)) = 1500*
(* here, rep 2 earns the difference between him/her and rep 7 because he is in the depth right above it / his/her parent)

**Rep 2 TOTAL earn = 460 + 1500 = 1,960**

... 等等


我本可以构建脚本来使用HEAVY mysql递归,并为每个深度简单地进行大量的while()循环,但发现它是不必要的,并且由于我使用存储过程进行操作并预先计算层次结构而对系统造成负担深度并适当地对数据进行排序。 计算最高级别的代表很简单,但是要从列表中的第二个人返回(依此类推),然后重新开始,这是我在努力的地方。

我希望能够根据上面的示例数据返回类似于以下内容的输出:

num | Name | earnAmount
------------------------
1   |  A   | 9940
2   |  B   | 1960
7   |  E   | 10500
3   |  C   | 1650

提前谢谢您的帮助!

有关已问问题的注释:

问:如何计算收入百分比?

答:它们不是计算出来的,而是由代表确定的特定ID,代表代表销售。


问:您如何确定“级别”或父母/子女关系?

答:我不在这个例子中。 这部分方程式是通过MySQL存储过程解决的,并且在许多方面都没有关系(我也可以显示每个rep的父repnum,但是由于数组是从上到下构建的,因此它可能不太有用)。 第一个表中示例数据的深度和排序顺序已经过格式化和布局,可以通过PHP中的简单print(results_array)语句打印每棵树。


问:第一个表中productID的相关性是什么?

答:每个销售代表都可以出售我们系统中可用productID分类的任何产品(数百种)。 每个销售代表还可以赚取该特定产品每次销售收入的一定百分比。 获利百分比与特定产品类型(尽管每种产品都有最高和最低获利)或特定深度(尽管深度较高的代表永远不会比其下线树的特定productID拥有较低的获利百分比)完全无关。


问:如何在第一个表中对数据进行排序?

答:一点无关紧要(请相信我),但在幕后我创建了一个面包屑列,该列会使用当前的repnum,然后附加子项并基于该子项和此时树的深度进行排序。 给出的示例:

0 (invisible parent which selects ALL sales reps)
  0-1
    0-1-2
      0-1-2-7
    0-1-3
...

这是我的方法。
我假设数组可以是一维的。 depth足以让我们确定某个代表是否有“ 孩子 ”。 所以数组看起来像这样:

$reps = array(
    array("rep" => "1", "name" => "A", "productId" => "1", "sales" => 5000, "earn" => 0.50, "depth" => "1"),
    array("rep" => "1", "name" => "A", "productId" => "2", "sales" => 10000, "earn" => 0.35, "depth" => "1"),
    array("rep" => "2", "name" => "B", "productId" => "1", "sales" => 400, "earn" => 0.40, "depth" => "2"),
    array("rep" => "2", "name" => "B", "productId" => "2", "sales" => 1000, "earn" => 0.30, "depth" => "2"),
    array("rep" => "7", "name" => "E", "productId" => "1", "sales" => 30000, "earn" => 0.35, "depth" => "3"),
    array("rep" => "3", "name" => "C", "productId" => "1", "sales" => 5000, "earn" => 0.33, "depth" => "2")
);

我决定采用递归方法。 我们在earn()函数中循环遍历数组,每次迭代后都会前进数组指针。 在函数内部,我们在一个for循环中再次迭代,从current + 1数组元素开始直至结尾查找Rep的“ 子代 ”。 代码如下所示:

function earn() {
    global $reps, $earns;
    $rep = current($reps);
    $key = key($reps);
    $immediateChildEarn = null;

    //basic Rep's earnings
    $earn = $rep['sales'] * $rep['earn'];

    //loop to find children with the same productId
    for ($i = $key + 1; $i < count($reps); $i++) {
        $child = $reps[$i];

        //we're only interested in Reps with the same product and deeper position
        if ($rep['productId'] !== $child['productId'] || $rep['depth'] >= $child['depth']) {
            continue;
        }

        //collect the earn of the immediate child
        if (null === $immediateChildEarn) {
            $immediateChildEarn = $child['earn'];
        }

        //the earn difference can't be greater than the difference between Rep and its first immediate child Rep
        if ($immediateChildEarn > $child['earn'] && $rep['depth'] + 1 < $child['depth']) {
            $child['earn'] = $immediateChildEarn;
        }

        //calculate the earnings gained from this child
        $earn += $child['sales'] * ($rep['earn'] - $child['earn']);
    }

    //just a quick fix to prevent throwing Notices - not significant for the algorithm itself
    if (!isset($earns[$rep['rep']])) {
        $earns[$rep['rep']] = 0;
    }

    $earns[$rep['rep']] += $earn;

    $finish = next($reps);
    if (false !== $finish) {
        earn();
    }
}

$earns = array();
reset($reps);
earn();

var_dump($earns)的结果将是:

Array
(
    [1] => 9940
    [2] => 1960
    [7] => 10500
    [3] => 1650
)

请随意评论我的答案。 我将尽力修复所有错误并改进代码。

复杂度
我不擅长计算算法的复杂度,但是在我的解决方案中,复杂度是我认为的:

  1. 时间复杂度
    • 最坏情况O(n logn)
    • 最佳情况O(2n)
  2. 内存复杂度(不包括输入数组)
    • 最坏情况O(n)
    • 最佳情况O(1)

如果我错了,请随时纠正我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM