如何获取标记'goldenKingCobra'
<livestock>
<goldenKingCobra name="Indus" age="4" sex="f" />
<goldenKingCobra name="Nile" age="8" sex="f" />
<goldenKingCobra name="Tigris" age="9.5" sex="f" />
</livestock>
我需要数组格式的值
阵列(0 =&GT;阵列(&#34;名称&#34; =&GT;&#34;梧桐&#34;&#34;年龄&#34; =&GT;&#34; 4&#34; ,&#34;性别&#34; =&gt;&#34; f&#34;),1 =&gt;数组(&#34;名称&#34; =&gt;&#34;尼罗&#34;,&# 34;年龄&#34; = GT;&#34; 8&#34;&#34;性别&#34; = GT;&#34; F&#34), 2 =&GT;阵列(&#34;名称&#34; =&GT;&#34;格里斯&#34;&#34;年龄&#34; =&GT;&#34; 9.5&#34;&#34 ;性别&#34; = GT;&#34; F&#34));
请帮帮我
答案 0 :(得分:0)
xml_parse_into_struct()
。请参阅:http://php.net/manual/es/function.xml-parse-into-struct.php
<?php
$xml = "<livestock>
<goldenKingCobra name="Indus" age="4" sex="f" />
<goldenKingCobra name="Nile" age="8" sex="f" />
<goldenKingCobra name="Tigris" age="9.5" sex="f" />
</livestock>";
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
// array of indexes
print_r($index);
// array of values
print_r($vals);
?>
答案 1 :(得分:0)
试试这个
$data = '<livestock>
<goldenKingCobra name="Indus" age="4" sex="f" />
<goldenKingCobra name="Nile" age="8" sex="f" />
<goldenKingCobra name="Tigris" age="9.5" sex="f" />
</livestock>';
$xmlData = new SimpleXMLElement($data);
foreach($xmlData as $x){
echo $x['name'].'<br>';
echo $x['age'].'<br>';
echo $x['sex'].'<br>';
}