views:

52

answers:

1

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

Like

 1. date    1343360810     objectId 1628
    field 10 value 3
 1. date    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"
  }
+2  A: 

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;
Kaivosukeltaja
where do you get $change_percentage = $changes / count($objects) * 100;$objects ? or is it $object_array or $obj ?
streetparade
where do you get objects from?
streetparade
In this example $object_array was originally named $objects, I renamed it for clarity but forgot to change the last row. Fixed, thanks for the heads up!
Kaivosukeltaja
...and $object_array refers to the array which holds your data. If it's not in array form (MySQL resource, for instance) you'll need to replace the foreach() with whatever you use to iterate through the data.
Kaivosukeltaja
Thank you very much this worked for me have a nice day :-)
streetparade