【问题标题】:Method for recursively calculating values from a PHP array?从 PHP 数组递归计算值的方法?
【发布时间】:2013-10-17 19:38:36
【问题描述】:

这个问题与层次数据有些相关,但我的数据库中已经有一个存储过程,它返回下面提到的数据。我不是试图找出如何构建层次结构(完成),我试图找出使用 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(results) 循环中,我试图为返回的每个 repnum 计算以下内容:

  • 该代表的总销售额乘以每种产品的收入百分比(他们的总销售额和收入金额)
  • 每个下线树的总销售额乘以当前代表与其下级之间的差值

展示其工作原理的数学:

代表 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 确定的。


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

A:我不在这个例子中。这部分等式是通过 MySQL 存储过程处理的,并且在很多方面不相关(我也可以显示每个 rep 的父 repnum,但由于数组是自上而下构建的,它可能不太有用)。第一个表中示例数据的深度和排序顺序已经被格式化和布局,使得每棵树都可以通过 PHP 中的简单 print(results_array) 语句打印出来。


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

答:每个代表都可以销售我们系统中可用的任何产品(数百个),按 productID 分类。每个销售代表还从该特定产品的每次销售中赚取一定百分比。收入百分比与特定产品类型(尽管每种产品都有最大和最小收入)或特定深度(尽管深度较高的代表从不的收入百分比较低)完全无关具体 productID 比他的下线树)。


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

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

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

【问题讨论】:

  • 嗯.. 这是某种形式的阶梯式收入,您可以从您带来的其他人的销售中获得奖金?例如卖家 7 可以预期有多少不同的条目。他可以在第 0 级和第 3 级上吗?
  • 你的 repnums 是如何与他们的父母联系起来的?我在您的树中看到它,但在顶部的结果表中没有...?是否纯粹基于depth列和返回结果的顺序?
  • @scrowler - 通过父 ID,但此处不一定需要,因为该信息已用于创建层次结构表(包括上述深度)。虽然如果需要我也可以包含父 ID,但我认为没有必要,因为结果是按顺序给出的。
  • @JelleFerwerda - 不一定是奖金,基于分层佣金的销售计划。卖家 7 可以拥有与他/她销售的产品类型一样多的条目。在上面的树中,他永远不会超过他父母的深度/水平,但如果“树”是从他开始的,那么是的,我想他会被认为是在 0 级
  • 请添加实际数据排序的示例。它在数组中吗?或者如何知道水平?我想你创建的是一个重度嵌套的数组?

标签: php arrays recursion


【解决方案1】:

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

$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)

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

【讨论】:

  • 谢谢。我会仔细看看。值得注意的是,您对输入错误的评论不是错误。这在它后面的 * 中有解释。父母只能赚取与其直系子女之间的差额一样多的收入。在示例中,7 是 1 的下线代表,但 7 的直系子女(和父母)是 2。1 的收入永远不会超过 1 和 2 的差额,因为 2 将赚取他/她和 7 之间的差额。因此正确的差异是 0.50-0.40= 0.10。此外,Rep 2 的计算收入已关闭,因为它仅反映他/她的个人收入,而不是 7 的收入
  • 我错过了。嗯,它使算法复杂化了很多。我会尝试考虑这一点并编辑答案。
  • 我更新了代码。我希望现在它可以完全按照您的意愿工作。
  • 干得好。我决定不尝试它,因为我无法理解我的逻辑。正在研究类似的方法。
  • @matewka - 没关系,我看到了这个问题。在上面的示例数据中,我的代表 7 获得了 35% 的收入,而您的阵列让他获得了 40% 的收入,这将使代表 2 获得零覆盖收入。这是有道理的。我将继续与我的数据进行比较,但到目前为止一切都很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 2020-01-29
  • 2015-07-28
相关资源
最近更新 更多