2013-03-11 34 views
2

我有這樣的陣列從一個數組

array(2) { 
    [0] => 
    array(3) { 
    ['name'] => string(4) "John" 
    ['lastname'] => string(3) "Don" 
    ['pesel'] => string(6) "987987" 
    } 
    [1] => 
    array(3) { 
    ['name'] => string(4) "Mike" 
    ['lastname'] => string(5) "Evans" 
    ['pesel'] => string(5) "89779" 
    } 
} 

而且我想創建一個從上述陣列中的XML文件中創建XML,我用這個代碼創建一個XML文件

$student_info = array($resultant_array); 

// creating object of SimpleXMLElement 
$xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><student_info></student_info>"); 

// function call to convert array to xml 
array_to_xml($student_info,$xml_student_info); 

//saving generated xml file 
$xml_student_info->asXML('inxfo.xml'); 



// function defination to convert array to xml 
function array_to_xml($student_info, $xml_student_info) { 
    foreach($student_info as $key => $value) { 
      var_dump($value); 
      echo '<br />'; 
     if(is_array($value)) { 


      if(!is_numeric($key)){ 

       $subnode = $xml_student_info->addChild("$key"); 
       array_to_xml($value, $subnode); 

      } 
      else{ 

       array_to_xml($value, $xml_student_info); 
      } 
     } 
     else { 

      $xml_student_info->addChild("$key","$value"); 

     } 
    } 
} 

此代碼給英里解決這樣的

<student_info> 
    <name>John</name> 
    <lastname>Dozzn</lastname> 
    <pesel>987987</pesel> 
    <nme>Mike</name> 
    <lastname>Evans</lastname> 
    <pesel>89779</pesel> 
</student_info> 

但我想造成這樣的

<student_info> 
     <person> 
     <name>John</name> 
     <lastname>Dozzn</lastname> 
     <pesel>987987</pesel> 
     </person> 
     <person> 
     <name>Mike</name>   
     <lastname>Evans</lastname> 
     <pesel>89779</pesel> 
    </person> 
</student_info> 

如何添加額外的孩子到我的xml代碼?

+0

而就在'array_to_xml'功能與'包裝你的元素'在'foreach'循環中。 – simplyray 2013-03-11 09:55:47

+0

http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes/ – 2013-03-11 09:58:48

回答

1

在你的情況,改變else部分:

if(!is_numeric($key)){ 
    $subnode = $xml_student_info->addChild("$key"); 
    array_to_xml($value, $subnode); 
} 
else{ 
    $subnode = $xml_student_info->addChild("person"); 
    array_to_xml($value, $subnode); 
} 

但隨着安德烈建議,你可能想看看更多的通用功能。

編輯:我的版本的作品:

$student_info = array(
    array(
     'name' => "John", 
     'lastname' => "Don", 
     'pesel' => "987987", 
    ), 
    array(
     'name' => "Mike", 
     'lastname' => "Evans", 
     'pesel' => "89779", 
    ) 
); 

$xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><student_info></student_info>"); 

function array_to_xml($student_info, $xml_student_info) { 
    foreach($student_info as $key => $value) { 
     if(is_array($value)) { 
      if(!is_numeric($key)){ 
       $subnode = $xml_student_info->addChild("$key"); 
       array_to_xml($value, $subnode); 
      } 
      else{ 
       $subnode = $xml_student_info->addChild("person"); 
       array_to_xml($value, $subnode); 
      } 
     } 
     else { 
      $xml_student_info->addChild("$key","$value"); 
     } 
    } 
} 

array_to_xml($student_info, $xml_student_info); 

var_dump($xml_student_info->asXML()); 

輸出:

string '<?xml version="1.0"?> 
<student_info><person><name>John</name><lastname>Don</lastname><pesel>987987</pesel></person><person><name>Mike</name><lastname>Evans</lastname><pesel>89779</pesel></person></student_info> 

'(長度= 211)

+0

我想是這樣的,但我得到這個結果'約翰' – konadrian 2013-03-11 10:23:02

+0

我我想我需要看看你編輯的代碼。我嘗試了編輯並運行(請參閱編輯)。 – 2013-03-11 11:04:12

+0

我在代碼中發現錯誤,謝謝:)這是我的解決方案 – konadrian 2013-03-11 11:21:57