问题描述:

Is there any way to get all attributes and its values as an array.

Here I have a node

<vehicle wheels="four" color="red"/>

what i need is to get an array like

$vehicle = array("wheels" => "four", "color" => "red");

网友答案:

You can do it using SimpleXMLElement parsing.

$xml = '<vehicle wheels="four" color="red"/>';

$x = new SimpleXMLElement($xml);
$array = current($x->attributes());
print_r($array);
网友答案:

I found the solution by using attributes property.

Here is the code

foreach ($vehicle->attributes as $attribName => $attribute_node)
{
    $array[$attribName] = $attribute_node->nodeValue;
}

The $array will produce what is expecting...

相关阅读:
Top