有没有更优雅的方法将SimpleXML属性转义成数组?
$result = $xml->xpath( $xpath );
$element = $result[ 0 ];
$attributes = (array) $element->attributes();
$attributes = $attributes[ '@attributes' ];
我不想通过循环来提取键/值对。我只需要将它转换为数组然后传递即可。我本以为attributes()
默认就能做到,或者至少提供这个选项。但我甚至找不到上述解决方案的任何地方,我必须自己想出来。我是在过度复杂化问题还是什么?
编辑:
在确定访问@attributes数组是否安全之前,我仍然使用上述脚本。
current(...)? : []
,因为如果XML元素没有属性,current(...)
返回false
,而空数组(在我看来)更合适。 - Roman Hocke