A->b->c
might exist but c
might not exist. How do I check it?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You could try:
Using
if(isset($A->b){
gave me issues, so I triedif($A->b){
and it worked!If you have PHP 5.3, you can just use
$a->count()
. Otherwise, scippie's solution using@count($a->children())
works well. I find I don't need the @ but older PHP implementations may need it.Using xpath:
where
$parent
is an indirect or direct parent of the child node to check and$xpathToChild
is an xpath of the child relative to$parent
.()[1]
is because we don't want to select all the child nodes. One is enough.To check if $a->b->c exists:
has_child($a,'b/c');
You can also check for attributes. To check if the node
c
has thet
attribute.Method xpath returns array of matched elements or false
http://www.php.net/manual/ru/simplexmlelement.xpath.php
Simply