PHP对象变量名称?

Ste*_*ven 20 php

我有一个从1到10的循环并打印出值

$entity_object->field_question_1 到10点......

$entity_object->field_question_1,$entity_object->field_question_2

我想在这个循环中打印这个,我怎样才能获得变量?我试过了

$var = "entity_object->field_question_".$i;
print $$var;
Run Code Online (Sandbox Code Playgroud)

但那不起作用......

我怎样才能获得这些价值?

Tim*_*ers 40

这应该工作:

$var="field_question_$i";
$entity_object->$var;
Run Code Online (Sandbox Code Playgroud)


小智 20

实际上你需要将变量放在字符串之外,以便这些解决方案能够工作: $var="field_question_".$i;

$entity_object->$var;

要么

$entity_object->{"field_question_".$i}
Run Code Online (Sandbox Code Playgroud)


Sam*_*ane 9

首先,数组更适合您想要做的事情.

你回答的问题是: print $entity_object->{"field_question_$i"};