0

I have an object, when I do a var_dump this is the output:

array(7) {
  ["foodRaw"     ]=>  string(9) "000000000"
  ["broodRaw"    ]=>  string(9) "111000000"
  ["food"        ]=>  int(0)
  ["brood"       ]=>  int(3)
  ["size"        ]=>  int(0)
  ["foodHarvest" ]=>  int(0)
  ["broodHarvest"]=>  int(0)
}

When I do

foreach ($data AS $key => $Value){ 
    $this->{$key} = $Value;
    echo $this->{$key} . " = " . $Value . "<br>
    ";
}

IT doesn't behave like I expect, It ouputs this nonsense:

000000000 = 000000000<br>
111000000 = 111000000<br>
0 = 0<br>
3 = 3<br>
0 = 0<br>
0 = 0<br>
0 = 0<br>

Does anybody know the logic behind this behavior?

1 Answer 1

3

Well sure. You are setting:

$this->{$key} = $Value;

So now those both have the same value. If you immediately echo them:

echo $this->{$key} . " = " . $Value

You'll get the same output for both! This is like setting $a = $b and then being surprised when echo $a = $b shows you the same value for both.

I think what you want is:

echo $key . " = " . $Value
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.