XML 到数组?[PHP]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12148662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML to Array? [PHP]
提问by Ginzo Milani
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:
好吧,我在将 xml 改回数组时遇到了问题......如果每个 xml 都遵循相同的格式,但每个 XML 都是不同的,除了<Formula>标签、公式名称和移动速度 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 printssomething but I couldn't get past that or even echoit..
这实际上是prints一些东西,但我无法克服那个甚至echo它..
foreach($xml->text as $string) {
print_r($string);
echo 'attributes: '. $string->attributes() .'<br />';
}
Didn't work, originally it's for stringsbut none of them are strings...
没有用,最初是为了strings但没有一个是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 arraywithout knowing what exactly the value will be called..
也没有用,我需要这样的东西才能工作,这样我就可以使用 中的值,而array无需知道该值将被称为什么..
回答by jerdiggity
This is your best bet, and it should eliminate all the SimpleXMLElement objects and instead give you nothing but arrays:
这是您最好的选择,它应该消除所有 SimpleXMLElement 对象,而只提供数组:
$xml = simplexml_load_file("test.xml");
$xml_array = unserialize(serialize(json_decode(json_encode((array) $xml), 1)));
print_r($xml_array);
Makes the difference between this:
这之间的区别:


And this:
和这个:


Hope that helps... :)
希望有帮助... :)
回答by Ja?ck
You can't access children by using a foreach on the node itself, you need to use .children():
您无法通过在节点本身上使用 foreach 来访问子级,您需要使用.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}";
}
}
回答by mrok
for your example this code is enough:
对于您的示例,此代码就足够了:
$xml = simplexml_load_file('formula.xml');
$arr = (array) $xml;
var_dump($arr);
and your xml goes into array
formula.xml contains your xml
并且您的 xml 进入数组
formula.xml 包含您的 xml
回答by Roey
If you have cdata and and attributes in the same node, using the methods above will omit the attributes.
如果在同一个节点中有 cdata 和 and 属性,使用上面的方法将省略这些属性。
try using this method:
尝试使用这种方法:
function xml2array($xmlObject, $out = [])
{
foreach($xmlObject->attributes() as $attr => $val)
$out['@attributes'][$attr] = (string)$val;
$has_childs = false;
foreach($xmlObject as $index => $node)
{
$has_childs = true;
$out[$index][] = xml2array($node);
}
if (!$has_childs && $val = (string)$xmlObject)
$out['@value'] = $val;
foreach ($out as $key => $vals)
{
if (is_array($vals) && count($vals) === 1 && array_key_exists(0, $vals))
$out[$key] = $vals[0];
}
return $out;
}
$xml = simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA);
$arr = xml2array($xml);

