Using xpath:
function has_child(\SimpleXMLElement $parent=null, string $xpathToChild) { return isset($parent) && !empty($parent->xpath('('.$xpathToChild.')[1]')); }
where $parent is the indirect or direct parent of the child being checked, and $xpathToChild is the xpath of the child relative to $parent .
()[1] because we do not want to select all child nodes. One is enough.
To check if $ a-> b-> c exists:
has_child($a,'b/c');
You can also check attributes. To check if node c attribute t .
has_child($a,'b/c/@t');
CITBL Apr 18 '17 at 13:09 on 2017-04-18 13:09
source share