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

时间:2013-10-17 19:38:37

标签: php arrays recursion

这个问题在某种程度上与层次数据有关,但我已经有了一个来自我的数据库的存储过程,它返回下面提到的数据。我不是要找出如何构建层次结构(完成),我试图找出基于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(结果)循环中,我正在尝试为每个返回的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存储过程处理,并且在很多方面都不相关(我也可以为每个代表显示父repnum,但由于数组是从上到下构建的,所以它可能不太有帮助)。第一个表中示例数据的深度和排序顺序已经过格式化和布局,以便每个树都可以通过PHP中的简单print(results_array)语句打印。


  

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

     

答:每位代表都可以销售我们系统中可用的任何产品(数百种),按产品ID分类。每个销售代表还获得该特定产品的每次销售的百分比。收益%与特定产品类型完全无关(尽管每种产品有最大和最小收益)或特定深度(尽管更高深度的代表从不的收益率更低特定产品ID比他的下线树)。


  

问:您的数据如何在第一个表格中排序?

     答:有点无关紧要(只相信我),但在幕后我创建了一个获取当前repnum的面包屑列,然后根据它与树点深度的组合添加子节点和排序。给出的例子:

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

1 个答案:

答案 0 :(得分:4)

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

$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 的“ children ” 。代码看起来像这样:

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)
  3. 如果我错了,请随时纠正我。