我有一个从 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;
但这没有用......
我怎样才能得到这些值?
我有一个从 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;
但这没有用......
我怎样才能得到这些值?
这应该有效:
$var="field_question_$i";
$entity_object->$var;
实际上,您需要像这样将变量放在字符串之外,以便这些解决方案起作用:
$var="field_question_".$i;
$entity_object->$var;
或者
$entity_object->{"field_question_".$i}
首先,数组更适合你想做的事情。
你的问题的答案:print $entity_object->{"field_question_$i"};
升级到 PHP 7 时,我们遇到了如下语句的问题:
$variable->$node[$i] = true;
这在 PHP 5.4 中运行良好,但在 PHP 7 中导致整个网站崩溃。所以我们将其替换为:
$variable->{$node[$i]} = true;
解决问题。
或者您可以在数组和对象之间进行类型转换。
数组很简单,因为它们有组织且易于访问。对象完全不同,但与许多专业人士不同。
像这样设置你的对象:
$entity_object["field_question_{$i}"] = ''//value;
然后可以将它们类型转换为对象:
$entity_object = (object)$entity_object;
然后,您可以像这样引用它们:
$entity_object->field_question_1 ...;