Anthony Anthony - 1 year ago 65
PHP Question

How to appending variable to a variable name

I have a similar problem with this topic
Appending a value of a variable to a variable name?

but the answer not solve my problem,
i have variable,$item->ans_cont1 ..... to $item->ans_cont34

for($i=1;$i<35;$i++) {
$inputval = 'item->ans_cont'.$i;
echo $$inputval; //output is empty
echo ${'item->ans_cont'.$i}; //output is empty

but not work,is it "->" arrow problem?how to fix it?


Answer Source

$item is an Object so you have to call its attribute:

$inputval = $item->{'ans_cont'.$i};
// Or 
$inputval = 'ans_cont'.$i;
echo $item->$inputval;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download