Reputation: 11285
This is in Drupal but should be applicable for normal PHP.
field-event-address is an array that is under $entity_fetched
.
<?php if ($entity_fetched->field-event-address != "") echo $entity_fetched->field-event-address['und']['0']['value']; ?>
For some reason, when I do this, I get this error if I leave the ['und']['0']['value']
off:
Notice: Undefined property: stdClass::$field in eval() (line 7 of /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code).
Notice: Use of undefined constant event - assumed 'event' in eval() (line 7 of /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code).
Notice: Use of undefined constant address - assumed 'address' in eval() (line 7 of /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code).
And this error if I leave it on:
Parse error: syntax error, unexpected '[', expecting ',' or ';' in /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code on line 7
I am trying to create an if statement that checks to see if the array under the object is empty or not, and if it isn't, echos the array contents.
I'm really scratching my head with this one - I should be able to just check if an array is empty if it's under an object, correct?
Upvotes: 0
Views: 293
Reputation: 19
For Drupal, you really should be using entity metadata wrappers.
So your code would be something like:
$wrapper = entity_metadata_wrapper('entity_type', $fetched_entity);
$value = $wrapper->field_name->value();
if (!empty($value)) echo $value;
You can find more information about entity metadata wrappers here: https://drupal.org/node/1021556
Upvotes: 0
Reputation: 9765
-
sign can't be used in variables names in PHP. Because of that you're getting syntax error (or undefined constant when PHP is trying to understand what did you mean).
Instead you can change name to $entity_fetched->fieldEventAddress
(it's still readable and now it's correct).
Upvotes: 1