mercredi 21 décembre 2016

PHP Get and change a random item in an object

I know how to do this in an array, but not sure how to do it with an object. My object looks like this...

stdClass Object
(
    [Full] => 10
    [GK] => 10
    [Def] => 10
    [Mid] => 10
    [Att] => 0
    [Youth] => 0
    [Coun] => 0
    [Diet] => 10
    [Fit] => 0
    [Promo] => 10
    [Y1] => 0
    [Y2] => 9
    [Y3] => 0
    [IntScout] => 0
    [U16] => 0
    [Physio] => 4
    [Ground] => 1
)

I need to do a loop, checking that the total of the values isn't greater than a certain value (50 in this case). If it is, I need to take one of them at random and reduce it by 1, until eventually the total is no higher than 50.

My failed attempt so far is :-

$RandomTrainer = mt_rand(0, (count($this->Trainers) -1));
if ($this->Trainers->$RandomTrainer] > 0) {
$this->Trainers->$RandomTrainer] -= 1;

Obviously that doesn't work because it's looking for '0' or some number in the object, which isn't there.

I've skipped the loop / total part because that's working at my end.




Aucun commentaire:

Enregistrer un commentaire