开发者

Getting the Percentage of changes

开发者 https://www.devze.com 2022-12-09 07:21 出处:网络
How can i get the percentage of the changes which happened? Like 1. date1343360810objectId 1628 field 10 value 3

How can i get the percentage of the changes which happened?

Like

 1. date    1343360810     objectId 1628
    field 10 value 3
 1. date    开发者_JAVA百科1242360811     objectId 1628
    field 10 value 5
 1. date    1243364812     objectId 1628
    field 10 value 5
 1. date    1240360814     objectId 1628
    field 10 value 5

This would mean the value has ben changed 1 time in 4 objects. This would result a percentage of 25% .

My question is how can i do that in PHP.

I have Objects like this an.

  [69]=>
  object(stdClass)#92 (6) {
    ["id"]=>
    string(7) "1824709"
    ["objectId"]=>
    string(4) "1628"
    ["type"]=>
    string(1) "0"
    ["field"]=>
    string(2) "10"
    ["value"]=>
    string(1) "3"
    ["date"]=>
    string(10) "1243360814"
  }

[70]=>
  object(stdClass)#93 (6) {
    ["id"]=>
    string(7) "1826225"
    ["objectId"]=>
    string(4) "1628"
    ["type"]=>
    string(1) "0"
    ["field"]=>
    string(2) "10"
    ["value"]=>
    string(1) "0"
    ["date"]=>
    string(10) "1243360814"
  }


Iterate the array of objects and keep track when the value changes. Something like this should do:

$oldvalue = false; // This is to exclude the first value from registering as a change
$changes = 0;
foreach($object_array as $obj) {
  if($oldvalue !== false && $obj->value != $oldvalue)
    $changes++;
  $oldvalue = $obj->value;
}
$change_percentage = $changes / count($object_array) * 100;
0

精彩评论

暂无评论...
验证码 换一张
取 消