doupao5296 2013-10-08 15:16
浏览 35

检查作为对象一部分的数组是否为空

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?

  • 写回答

2条回答 默认 最新

  • dongpa6867 2013-10-08 15:20
    关注

    - 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).

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助