I have a variable stack in object form:
I also have an array of data that when applicable, I want to replace those values. The array has things like: Lets call the array $stats
What I have been trying to accomplish (and this was using the double variable, is replace the value in the original object stack with an updated value if in the array.
foreach ($stats as $key => $value) {
$data = "people->" . $key;
$$data = "<strong>" . $$data . "</strong>";
}
But, that just doesn't look like it should work, and isn't as clean as I would like it. Is there a better way to handle something like this?
Thanks for any help that can be provide.
^^^
Update:
Basically I am trying to wrap around a variable, if the variable shows up in the array. And, then have it print like the original value, except with the wrapped around it.
foreach ($arrayvariable as $key => $val) {
$value->$key = "<strong>" . $value->$key . "</strong>";
}
I suspect you meant
$$data = "<strong>" . $value . "</strong>";
in your question, which would translate to
$value->$key = "<strong>" . $val . "</strong>";
$value = new stdClass();
$value->one = 1;
$value->two = 2;
$value->three = 3;
$value->four = 4;
$arr = array();
$arr['one'] = 11;
$arr['four'] = 14;
foreach($value as $key => &$val){
if(isset($arr[$key])) {
$val = $arr[$key];
}
}
var_dump($value); // object(stdClass)#1 (4) { ["one"]=> int(11) ["two"]=> int(2) ["three"]=> int(3) ["four"]=> &int(14) }
If $obj
is your object, you can do this.
$key = 'four';
echo $obj->$key;