[英]How to assign field name dynamically in Drupal
I have Drupal 7
site. 我有Drupal 7
网站。 It has custom content type node which has 25+ fields of which 10 fields have very similar name. 它具有自定义内容类型节点,该节点具有25个以上的字段,其中10个字段的名称非常相似。
In a scenario, I need to loop through n products & need to get the different fields values. 在一种情况下,我需要遍历n个产品并需要获取不同的字段值。
In order to avoid if-else chain, I am thinking to construct the field name dynamically in the following way. 为了避免if-else链,我正在考虑通过以下方式动态构造字段名称。
function GetProduct($node,$a)
{
$fieldName = "field_product_" . $a;
print_r( $node->$fieldName[LANGUAGE_NONE][0]['value'] ); // not getting value
}
I am using php v 5.5.12 我正在使用PHP v 5.5.12
How to get the field value in this way? 如何以这种方式获取字段值?
Any help highly appreciated. 任何帮助,高度赞赏。
您只不过忘记了括号:)使用$node->{$fieldName}
而不是$node->$fieldName
:
print_r( $node->{$fieldName}[LANGUAGE_NONE][0]['value'] ); // works
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.