PHP attributes() Function
Example
Return the attributes and values for the XML <body> element:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body date="2014-01-01" type="private">Do
not forget me this weekend!</body>
</note>
XML;
$xml = simplexml_load_string($note);
foreach($xml->body[0]->attributes() as $a => $b)
{
echo $a,'="',$b,"<br>";
}
?>
Run Example »
Definition and Usage
The attributes() function returns the attributes and values of an XML element.
Syntax
SimpleXMLElement::attributes(ns, prefix)
Parameter Values
Parameter | Description |
---|---|
ns | Optional. Specifies a namespace for the retrieved attributes |
prefix | Optional. Specifies TRUE if ns is a prefix and FALSE if ns is a URI. Default is FALSE |
Technical Details
Return Value: | A SimpleXMLElement object on success |
---|---|
PHP Version: | 5.0+ |
❮ PHP SimpleXML Reference