2012-09-07 67 views
2

我有一個簡單的xml對象,這是Zend_Rest_Client_Result對象的結果。從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> 

如何使用PHP從上述XML對象中創建類似這樣的數組?

$userDetails = array(); 
$userDetails["name"] = "jake"; 
$userDetails["type"] = "user"; 
$userDetails["fname"] = "Jake"; 
$userDetails["lname"] = "Gordon"; 
$userDetails["phone"][0] = "123-555-1234"; 
$userDetails["phone"][1] = "123-555-1234"; 

感謝,

回答

1
$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); 
+0

優秀的答案!謝謝 – Jake

+0

Quick Question ..如果fname或lname或phone屬性不存在,PHP會給出警告,但在foreach循環中使用。我怎樣才能保護我的代碼免受這個影響? – Jake

+0

在foreach循環之前將xpath搜索的結果存儲到一個變量中,並對變量不是空的進行測試。看到我更新的答案。 – Scuzzy

0
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; 
} 
?> 

另一種快速和骯髒的方法是:

<?php 
    $a = json_decode(json_encode((array) simplexml_load_string($s)),1); 
?> 

這是不太可靠的,並會造成問題,你代碼中包含CDATA節點,以及。

+0

努力工作。但太難以理解正在發生什麼.. – Jake