数组计算PHP

时间:2018-04-07 15:35:28

标签: php

我再次陷入PHP数组计算,如果有人理解我的问题请尝试帮助我,让我们解释一下......

[data.txt content]

Peer             Call ID      Duration Recv: Pack  Lost       (     %) Jitter Send: Pack  Lost       (     %) Jitter
139.59.232.196   0bb9262d6a1  00:01:12 0000003558  0000000000 ( 0.00%) 0.0000 0000001177  0000000000 ( 0.00%) 0.0200
139.59.232.196   41283499492  00:00:00 0000000000  0000000000 ( 0.00%) 0.0000 0000000000  0000000000 ( 0.00%) 0.0000
139.59.232.196   7033a541240  00:00:08 0000000000  0000000000 ( 0.00%) 0.0000 0000000019  0000000000 ( 0.00%) 0.0000
3 active SIP channels

PHP代码从这里开始。

$data = file_get_contents('./data.txt', TRUE);
$lines = explode("\n", $data);

foreach ($lines as $line) {
    if (!preg_match('/( 0.00%)/', $line)) {
        continue;
    }
    $data = explode(' ', $line);
    $list[] = $data;
}

foreach ($list as $qoscalc) {
    $average[] = ($qoscalc[17] * 1000 / 2);
    $jitter[] = (int)$qoscalc[14];
    $packet_loss[] = (int)$qoscalc[13];
}
print_r($average);

直到这里代码工作正常它给了我$average数组的输出这个>>

Array ( [0] => 10 [1] => 0 [2] => 0 )

之后,我无法使用数组进行数学运算,如果我将它们转换为变量&我只使用1个数据然后代码工作正常,但是当我试图得到所有我无法做到的结果时,如果有人理解我的问题,请帮助我。

$effective_latency = ($average + $jitter * 2 + 10 );

if ($effective_latency < 160) {
    $r_value = 93.2 - ($effective_latency / 40);
} else {
    $r_value = 93.2 - ($effective_latency - 120) / 10;
}

$r_value = $r_value - ($packet_loss * 2.5);

$mosresult = 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);

$moslist[] = $mosresult;

我想获得所有3个数组结果,它假设如下例:Array([0] =&gt; 4.40372901469 [1] =&gt; 3.40372901469 [2] =&gt; 4.90372901469)

$i = 0; $t = 0; $e = 0; $g = 0; $f = 0; $p = 0; $b = 0;

foreach ($moslist as $mos) {
    $i++;
    if ($mos <= "5") {
        $qosq = 'Excellent';
        $e++;       
    } else if ($mos <= "4") {
        $qosq = 'Good';
        $g++;       
    } else if ($mos < "3") {
        $qosq = 'Fair';
        $f++;       
    } else if ($mos <= "2") {
        $qosq = 'Poor';
        $p++;       
    } else if ($mos <= "1") {
        $qosq = 'Bad';
        $b++;       
    } else {
        continue;
    }
    $t++;
}
echo $qosq, "<br><br>\n";

2 个答案:

答案 0 :(得分:0)

如果我理解正确的问题,最简单的解决方案是创建一个计算mosResult的函数

function getMosResult($average,$jitter,$packet_loss)
{
  $effective_latency = ($average + $jitter * 2 + 10 );

  if ($effective_latency < 160) {
      $r_value = 93.2 - ($effective_latency / 40);
  } else {
      $r_value = 93.2 - ($effective_latency - 120) / 10;
  }

  $r_value = $r_value - ($packet_loss * 2.5);

  return 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);
}

然后在for循环中将函数应用于所有结果,如此

$length = count($list);
for($i = 0;$i < $length;$i++){ 
    $mosList[]=getMosResult($average[$i],$jitter[$i],$packet_loss[$i]);
}

这个解决方案不适合大型项目,因为您很快就会失去三个源阵列之间的一致性。要获得更加可靠的解决方案,请查看对象或至少关联数组。

答案 1 :(得分:0)

我认为您基本上可以将所有代码放在一个循环中:

foreach ($lines as $line) {
    if (!preg_match('/( 0.00%)/', $line)) {
        continue;
    }
    $data = explode(' ', $line);
    $average = ($data[17] * 1000 / 2);
    $jitter = (int)$data[14];
    $packet_loss = (int)$data[13];
    $effective_latency = ($average + $jitter * 2 + 10 );
    if ($effective_latency < 160) {
        $r_value = 93.2 - ($effective_latency / 40);
    } else {
        $r_value = 93.2 - ($effective_latency - 120) / 10;
    }
    $r_value = $r_value - ($packet_loss * 2.5);
    $mosresult = 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);
    $i++;
    if ($mosresult > 4) {
        $qosq = 'Excellent';
        $e++;       
    } else if ($mosresult > 3) {
        $qosq = 'Good';
        $g++;       
    } else if ($mosresult > 2) {
        $qosq = 'Fair';
        $f++;       
    } else if ($mosresult > 1) {
        $qosq = 'Poor';
        $p++;       
    } else if ($mosresult > 0) {
        $qosq = 'Bad';
        $b++;       
    } else {
        continue;
    }
    $t++;
    echo $qosq, "<br><br>\n";
}