我有以下数组:
array (size=8) 0 => array (size=5) 'entity_id' => int 571962 'time' => int 1671101084788 'time to datetime' => string '2022-12-15 11:44:44' (length=19) 'PhaseId_new' => string 'Close' (length=5) 'PhaseId_old' => string 'Accept' (length=6) 1 => array (size=5) 'entity_id' => int 571962 'time' => int 1671100537178 'time to datetime' => string '2022-12-15 11:35:37' (length=19) 'PhaseId_new' => string 'Accept' (length=6) 'PhaseId_old' => string 'Fulfill' (length=7) 2 => array (size=5) 'entity_id' => int 571962 'time' => int 1671100012012 'time to datetime' => string '2022-12-15 11:26:52' (length=19) 'PhaseId_new' => string 'Fulfill' (length=7) 'PhaseId_old' => string 'Review' (length=6) 3 => array (size=5) 'entity_id' => int 571962 'time' => int 1671099984979 'time to datetime' => string '2022-12-15 11:26:24' (length=19) 'PhaseId_new' => string 'Review' (length=6) 'PhaseId_old' => string 'Accept' (length=6) 4 => array (size=5) 'entity_id' => int 571962 'time' => int 1671099802675 'time to datetime' => string '2022-12-15 11:23:22' (length=19) 'PhaseId_new' => string 'Accept' (length=6) 'PhaseId_old' => string 'Fulfill' (length=7) 5 => array (size=5) 'entity_id' => int 571962 'time' => int 1671027321749 'time to datetime' => string '2022-12-14 15:15:21' (length=19) 'PhaseId_new' => string 'Fulfill' (length=7) 'PhaseId_old' => string 'Approve' (length=7) 6 => array (size=5) 'entity_id' => int 571962 'time' => int 1671011168777 'time to datetime' => string '2022-12-14 10:46:08' (length=19) 'PhaseId_new' => string 'Approve' (length=7) 'PhaseId_old' => string 'Log' (length=3) 7 => array (size=5) 'entity_id' => int 571962 'time' => int 1671011166077 'time to datetime' => string '2022-12-14 10:46:06' (length=19) 'PhaseId_new' => string 'Log' (length=3) 'PhaseId_old' => null
我使用 entity_id
重新分组了每个子数组:
$result = array(); foreach ($data as $element) { //var_dump($element); $result[$element['entity_id']][] = $element; }
哪个输出我:
array (size=1) 571962 => array (size=8) 0 => array (size=5) 'entity_id' => int 571962 'time' => int 1671101084788 'time to datetime' => string '2022-12-15 11:44:44' (length=19) 'PhaseId_new' => string 'Close' (length=5) 'PhaseId_old' => string 'Accept' (length=6) 1 => array (size=5) 'entity_id' => int 571962 'time' => int 1671100537178 'time to datetime' => string '2022-12-15 11:35:37' (length=19) 'PhaseId_new' => string 'Accept' (length=6) 'PhaseId_old' => string 'Fulfill' (length=7) 2 => array (size=5) 'entity_id' => int 571962 'time' => int 1671100012012 'time to datetime' => string '2022-12-15 11:26:52' (length=19) 'PhaseId_new' => string 'Fulfill' (length=7) 'PhaseId_old' => string 'Review' (length=6) 3 => array (size=5) 'entity_id' => int 571962 'time' => int 1671099984979 'time to datetime' => string '2022-12-15 11:26:24' (length=19) 'PhaseId_new' => string 'Review' (length=6) 'PhaseId_old' => string 'Accept' (length=6) 4 => array (size=5) 'entity_id' => int 571962 'time' => int 1671099802675 'time to datetime' => string '2022-12-15 11:23:22' (length=19) 'PhaseId_new' => string 'Accept' (length=6) 'PhaseId_old' => string 'Fulfill' (length=7) 5 => array (size=5) 'entity_id' => int 571962 'time' => int 1671027321749 'time to datetime' => string '2022-12-14 15:15:21' (length=19) 'PhaseId_new' => string 'Fulfill' (length=7) 'PhaseId_old' => string 'Approve' (length=7) 6 => array (size=5) 'entity_id' => int 571962 'time' => int 1671011168777 'time to datetime' => string '2022-12-14 10:46:08' (length=19) 'PhaseId_new' => string 'Approve' (length=7) 'PhaseId_old' => string 'Log' (length=3) 7 => array (size=5) 'entity_id' => int 571962 'time' => int 1671011166077 'time to datetime' => string '2022-12-14 10:46:06' (length=19) 'PhaseId_new' => string 'Log' (length=3) 'PhaseId_old' => null
现在我需要计算每个阶段的持续时间(记录、批准、履行、接受、审核)。
例如:
日志:1671011168777 - 1671011166077 = 2700
批准:1671027321749 - 1671011168777 = 16152972
实现:(1671100537178 - 1671100012012) + (1671099802675 - 1671027321749) = 73006092
接受:(1671101084788 - 1671100537178) + (1671099984979 - 1671099802675) = 729914
评论:1671100012012 - 1671099984979 = 27033
我能够使用以下方法解析每个阶段:
foreach($result as $key => $val){ //var_dump($key); foreach($val as $key2 => $val2){ if($val2['PhaseId_new'] == 'Fulfill' or $val2['PhaseId_old'] == 'Fulfill'){ // var_dump($val2); } } }
但是我不清楚如何计算每个阶段的持续时间。
预期结果如下:
array (size=1) 571962 => array (size=8) 'Log' => int 2700 'Approve' => int 16152972 'Fulfill' => int 73006092 'Accept' => int 729914 'Review' => int 27033
PHP在线:https://onlinephp.io/c/2270e
工作流程:
尝试这样的事情。
这给你:
好吧,这花了我比我想要的更长的时间,但我得到了结果。首先是代码:
参见:https://onlinephp.io/c/92d7f
结果是:
现在解释一下:
首先,您的数据数组看起来是颠倒的,因此我使用
array_reverse()
来解决它。这里的假设是存在逻辑顺序。由于可能需要将多个周期相加,因此我使用一个名为
$durations
的数组来将它们相加。然后在内部循环中,如果有旧的阶段 ID,我可以计算持续时间并将其相加。最后我记得旧的阶段,因为我在循环的下一次迭代中需要它。
我还重命名了很多东西,使其名称能够传达变量的内容。