[英]Can't figure out how to determine if a SimpleXML node is empty
所以我有這個代碼:
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文件,但它似乎存儲了每個ID,無論節點是否存在於Passage
。
如果你使用empty()
會發生什么
if( empty($PrintQuestion->content->multichoice->feedback->hint->Passage) ) {
fwrite($fp, "<contentid filename=\"$value\">" . $PrintQuestion->attributes()->id . "</contentid>");
}
您可以將值轉換為字符串並與空字符串進行比較(類似於如何檢查simplexml對象的值 ):
$value = $root->child;
if ((string)$value === '') {
echo 'This passes if the child element existed, but was empty';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.