php SimpleXML 检查孩子是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1560827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php SimpleXML check if a child exists
提问by An employee
A->b->cmight exist but cmight not exist. How do I check it?
A->b->c可能存在但c可能不存在。我该如何检查?
采纳答案by Pim Jager
if($A->b->c != null) //c exists
If cdoes not exist, its value will be null(or, to be more precise, it will have no value). Note, however, that for this to work, both Aand bneed to not be null. Otherwise, PHP will throw an error (I think).
如果c不存在,它的价值将是null(或者,更准确地说,它将没有价值)。但是请注意,对于这个工作,既A和b需要不null。否则,PHP 会抛出错误(我认为)。
回答by null
It might be better to wrap this in an isset()
最好将它包装在一个 isset()
if(isset($A->b->c)) { // c exists
That way if $Aor $A->bdon't exist... it doesn't blow up.
这样,如果$A还是$A->b不存在......它不会炸毁。
回答by CedCannes
SimpleXML always return Object. If there is no child, empty object is returned.
SimpleXML 总是返回对象。如果没有孩子,则返回空对象。
if( !empty($a->b)){
var_dump($a->b);
}
回答by scippie
I solved it by using the children()function and doing a count()on it, ignoring an PHP error if there are no children by putting an @ before the count-call. This is stupid, but it works:
我通过使用该children()函数并对其执行 a 来count()解决它,如果没有孩子,则通过在计数调用之前放置 @ 来忽略 PHP 错误。这是愚蠢的,但它的工作原理:
$identification = $xml->identification;
if (@count($identification->children()) == 0)
$identification = $xml->Identification;
I hate this...
我讨厌这个...
回答by Artur Bodera
After some experimentation, I've discovered that the only reliable method of checking if a node exists is using count($xml->someNode).
经过一些实验,我发现检查节点是否存在的唯一可靠方法是使用count($xml->someNode).
Here's a test case: https://gist.github.com/Thinkscape/6262156
这是一个测试用例:https: //gist.github.com/Thinkscape/6262156
回答by user1391077
Method xpath returns array of matched elements or false
方法 xpath 返回匹配元素的数组或 false
if(false !== $A->xpath('b/c')) { ...
回答by Alex Matulich
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.
如果你有 PHP 5.3,你可以使用$a->count(). 否则,使用 scippie 的解决方案@count($a->children())效果很好。我发现我不需要 @ 但较旧的 PHP 实现可能需要它。
回答by O?uz Can Sertel
Simply
简单地
var_dump(count($xml->node));
回答by CITBL
Using xpath:
使用 xpath:
function has_child(\SimpleXMLElement $parent=null, string $xpathToChild)
{
return isset($parent) && !empty($parent->xpath('('.$xpathToChild.')[1]'));
}
where $parentis an indirect or direct parent of the child node to check and $xpathToChildis an xpath of the child relative to $parent.
其中$parent是要检查的子节点的间接或直接父节点,$xpathToChild是相对于的子节点的 xpath $parent。
()[1]is because we don't want to select all the child nodes. One is enough.
()[1]是因为我们不想选择所有的子节点。一个就够了。
To check if $a->b->c exists:
要检查 $a->b->c 是否存在:
has_child($a,'b/c');
has_child($a,'b/c');
You can also check for attributes. To check if the node chas the tattribute.
您还可以检查属性。检查节点c是否具有该t属性。
has_child($a,'b/c/@t');
回答by Daydah
Using if(isset($A->b){gave me issues, so I tried
if($A->b){and it worked!
使用if(isset($A->b){给我带来了问题,所以我尝试了
if($A->b){,它奏效了!
回答by phse
I use a helper function to check if a node is a valid node provided as a parameter in function.
我使用辅助函数来检查节点是否是作为函数参数提供的有效节点。
private static function isValidNode($node) {
return isset($node) && $node instanceof SimpleXMLElement && !empty($node);
}
Usage example:
用法示例:
public function getIdFromNode($node) {
if (!self::isValidNode($node)) {
return 0;
}
return (int)$node['id'];
}

