无法弄清楚如何确定SimpleXML节点是否为空

Thi*_*its 3 php xml simplexml

所以我有这个代码:

foreach ($xml->PrintQuestion as $PrintQuestion) {

     //if hint doesn't exist, store question id
     if (!$PrintQuestion->content->multichoice->feedback->hint->Passage) {

         fwrite($fp, "<contentid filename=\"$value\">" . $PrintQuestion->attributes()->id . "</contentid>");

     }

}
Run Code Online (Sandbox Code Playgroud)

基本上,如果Passage节点存在,我正在尝试将ID保存到XML文件,但它似乎存储每个ID,无论节点是否存在Passage.

Bay*_*ae' 7

如果你使用会发生什么 empty()

if( empty($PrintQuestion->content->multichoice->feedback->hint->Passage) ) {

    fwrite($fp, "<contentid filename=\"$value\">" . $PrintQuestion->attributes()->id . "</contentid>");      

}
Run Code Online (Sandbox Code Playgroud)

  • `empty`不适用于属性.如果节点`$ currentService-> HotelInfo-> Destination`为null,则此指令会出错:`if(!empty($ currentService-> HotelInfo-> Destination-> attributes() - > code))` (3认同)