所以我有这个密码:

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>");

     }

}

基本上,如果Passage节点存在,我试图将id保存到xml文件中,但不管Passage中是否存在节点,它似乎都在存储每个id。

最佳答案

如果使用empty()

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

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

}

09-17 13:58