Php 使用XML数据创建数组
作为Zend_Rest_Client_result对象的结果,我有一个这样的简单xml对象Php 使用XML数据创建数组,php,xml,zend-framework,dom,xml-parsing,Php,Xml,Zend Framework,Dom,Xml Parsing,作为Zend_Rest_Client_result对象的结果,我有一个这样的简单xml对象 <userdetails> <name value="jake"/> <type value="user"/> <attribute name="fname"> <value>Jake</value> </attribute> <attribute name="lname">
<userdetails>
<name value="jake"/>
<type value="user"/>
<attribute name="fname">
<value>Jake</value>
</attribute>
<attribute name="lname">
<value>Gordon</value>
</attribute>
<attribute name="phone">
<value>123-555-1234</value>
<value>999-888-7777</value>
</attribute>
</userdetails>
谢谢,函数xmlstr到数组($xmlstr){
function xmlstr_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
$child = $node->childNodes->item($i);
$v = domnode_to_array($child);
if(isset($child->tagName)) {
$t = $child->tagName;
if(!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
}
elseif($v) {
$output = (string) $v;
}
}
if(is_array($output)) {
if($node->attributes->length) {
$a = array();
foreach($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['@attributes'] = $a;
}
foreach ($output as $t => $v) {
if(is_array($v) && count($v)==1 && $t!='@attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
?>
$doc=新的DOMDocument();
$doc->loadXML($xmlstr);
将domnode_返回到_数组($doc->documentElement);
}
函数domnode_到_数组($node){
$output=array();
开关($node->nodeType){
案例XML\U CDATA\U节\U节点:
案例XML_文本_节点:
$output=trim($node->textContent);
打破
案例XML_元素_节点:
对于($i=0,$m=$node->childNodes->length;$ichildNodes->item($i);
$v=domnode_到_数组($child);
if(isset($child->tagName)){
$t=$child->tagName;
如果(!isset($output[$t])){
$output[$t]=array();
}
$output[$t][]=$v;
}
埃尔塞伊夫(五美元){
$output=(字符串)$v;
}
}
if(is_数组($output)){
如果($node->attributes->length){
$a=数组();
foreach($node->属性为$attrName=>$attrNode){
$a[$attrName]=(字符串)$attrNode->value;
}
$output['@attributes']=$a;
}
foreach($t=>v的输出){
if(is_数组($v)&&count($v)==1&&$t!='@attributes')){
$output[$t]=$v[0];
}
}
}
打破
}
返回$output;
}
?>
另一种快速而肮脏的方法是:
<?php
$a = json_decode(json_encode((array) simplexml_load_string($s)),1);
?>
这不太可靠,并且会给您编码包含CDATA节点的位置带来问题。$xml='JakeGordon123-555-1234999-888-7777';
$xml = '<userdetails><name value="jake"/><type value="user"/><attribute name="fname"><value>Jake</value></attribute><attribute name="lname"><value>Gordon</value></attribute><attribute name="phone"><value>123-555-1234</value><value>999-888-7777</value></attribute></userdetails>';
$simple = new SimpleXMLElement($xml);
$array = array();
if(isset($simple->name))
$array['name'] = (string) $simple->name->attributes()->value;
if(isset($simple->type))
$array['type'] = (string) $simple->type->attributes()->value;
if($foreach = $simple->xpath('//attribute[@name="fname"]/value'))
{
foreach($foreach as $node)
{
$array['fname'] = (string) $node;
}
}
if($foreach = $simple->xpath('//attribute[@name="lname"]/value'))
{
foreach($foreach as $node)
{
$array['lname'] = (string) $node;
}
}
if($foreach = $simple->xpath('//attribute[@name="phone"]/value'))
{
$array['phone'] = array();
foreach($simple->xpath('//attribute[@name="phone"]/value') as $node)
{
$array['phone'][] = (string) $node;
}
}
print_r($array);
$simple=新的simplexmlement($xml);
$array=array();
if(isset($simple->name))
$array['name']=(字符串)$simple->name->attributes()->value;
如果(isset($simple->type))
$array['type']=(字符串)$simple->type->attributes()->value;
如果($foreach=$simple->xpath('//attribute[@name=“fname”]/value'))
{
foreach($foreach作为$node)
{
$array['fname']=(字符串)$node;
}
}
如果($foreach=$simple->xpath('//attribute[@name=“lname”]/value'))
{
foreach($foreach作为$node)
{
$array['lname']=(字符串)$node;
}
}
如果($foreach=$simple->xpath('//attribute[@name=“phone”]/value'))
{
$array['phone']=array();
foreach($simple->xpath('//attribute[@name=“phone”]/value')作为$node)
{
$array['phone'][=(字符串)$node;
}
}
打印(数组);
Quick Question..PHP会在fname或lname或phone属性不存在但在foreach循环中使用时发出警告。如何保护我的代码不受此影响?将xpath搜索结果存储到foreach循环之前的变量中,并针对不为空的变量进行测试。请参阅我更新的答案。
$xml = '<userdetails><name value="jake"/><type value="user"/><attribute name="fname"><value>Jake</value></attribute><attribute name="lname"><value>Gordon</value></attribute><attribute name="phone"><value>123-555-1234</value><value>999-888-7777</value></attribute></userdetails>';
$simple = new SimpleXMLElement($xml);
$array = array();
if(isset($simple->name))
$array['name'] = (string) $simple->name->attributes()->value;
if(isset($simple->type))
$array['type'] = (string) $simple->type->attributes()->value;
if($foreach = $simple->xpath('//attribute[@name="fname"]/value'))
{
foreach($foreach as $node)
{
$array['fname'] = (string) $node;
}
}
if($foreach = $simple->xpath('//attribute[@name="lname"]/value'))
{
foreach($foreach as $node)
{
$array['lname'] = (string) $node;
}
}
if($foreach = $simple->xpath('//attribute[@name="phone"]/value'))
{
$array['phone'] = array();
foreach($simple->xpath('//attribute[@name="phone"]/value') as $node)
{
$array['phone'][] = (string) $node;
}
}
print_r($array);