本文整理匯總了PHP中DomDocument::hasAttributes方法的典型用法代碼示例。如果您正苦於以下問題:PHP DomDocument::hasAttributes方法的具體用法?PHP DomDocument::hasAttributes怎麽用?PHP DomDocument::hasAttributes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類DomDocument
的用法示例。
在下文中一共展示了DomDocument::hasAttributes方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: domToArray
/**
* From http://stackoverflow.com/questions/14553547/what-is-the-best-php-dom-2-array-function
*
* @param \DomDocument $root
* @return array
*/
protected function domToArray($root)
{
$result = [];
if ($root->hasAttributes()) {
$attrs = $root->attributes;
foreach ($attrs as $attr) {
$name = str_replace('cas:', '', $attr->name);
$result['attributes'][$name] = $attr->value;
}
}
if ($root->hasChildNodes()) {
$children = $root->childNodes;
if ($children->length == 1) {
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE) {
$result['value'] = $child->nodeValue;
return count($result) == 1 ? $result['value'] : $result;
}
}
$groups = [];
foreach ($children as $child) {
$childName = str_replace('cas:', '', $child->nodeName);
if (!isset($result[$childName])) {
$result[$childName] = $this->domToArray($child);
} else {
if (!isset($groups[$childName])) {
$result[$childName] = [$result[$childName]];
$groups[$childName] = 1;
}
$result[$childName][] = $this->domToArray($child);
}
}
}
return $result;
}