0

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");

1
  • what do you use to parse the XML, SimpleXML, DOMXPath/DOMDocument, sth else? Commented Jun 2, 2016 at 6:38

2 Answers 2

2

You can do it using SimpleXMLElement parsing.

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

$x = new SimpleXMLElement($xml);
$array = current($x->attributes());
print_r($array);
Sign up to request clarification or add additional context in comments.

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.