XML to Array? [PHP]
arrays, php, xml
Solution
You can't access children by using a foreach on the node itself, you need to use `.children()`:
$s =<<<EOS
<root>
<Formula>
<formulaname>Basic</formulaname>
<movespeed>1</movespeed>
<box>4</box>
<chicken>3</chicken>
<ducks>1</ducks>
<cereal>2</cereal>
</Formula>
</root>
EOS;
$xml = simplexml_load_string($s);
foreach ($xml->Formula as $element) {
foreach($element->children() as $key => $val) {
echo "{$key}: {$val}";
}
}
Problem
Well I been having issues changing an xml back into an array....It would be simple if each xml followed the same format but every XML is different with the exception of the `<Formula>` tag, formulaname and movespeed ex: ``` <Formula> <formulaname>Basic</formulaname> <movespeed>1</movespeed> <str>4</str> <dex>3</dex> <int>1</int> <will>2</will> </Formula> ``` or ``` <Formula> <formulaname>Basic</formulaname> <movespeed>1</movespeed> <box>4</box> <chicken>3</chicken> <ducks>1</ducks> <cereal>2</cereal> </Formula> ``` What I have tried: ``` $xml = simplexml_load_file("test.xml"); print_r($xml); ``` This actually `prints` something but I couldn't get past that or even `echo` it.. ``` foreach($xml->text as $string) { print_r($string); echo 'attributes: '. $string->attributes() .'<br />'; } ``` Didn't work, originally it's for `strings` but none of them are `strings`... ``` foreach ($xml->Formula as $element) { foreach($element as $key => $val) { echo "{$key}: {$val}"; } ``` Didn't work either, I needed something like this to work so I can use the values from the `array` without knowing what exactly the value will be called..