2014-11-04 57 views
0

我已閱讀stackoverflow這裏的其中一個問題,其中一位用戶與@Mifas和@Abhishek Madhani分享此link,這是行得通的。如何格式化php數組?

現在,我想格式化XML輸出是這樣的:

<Form xmlns="url" label="Home Visit Form" type="TextView" name="FormName"> 
    <Field name="Category" label="FormType" value="Form1" type="TextView"/> 
    <Field type="Delimiter"/> 
    <Field name="ContractNumber" label="Contract number" value="3300000011" type="TextView"/> 
    <Field type="Delimiter"/> 
    <Field name="ClientName" label="Name of Client" value="Neplatic Neplaticovic" type="TextView"/> 
    <Field name="BirthDate" label="Birthday" value="1980-07-04" type="TextView"/> 
    <Field name="DueDate" label="Due Date" value="2014-07-24" type="TextView"/> 
</Form> 

但我不知道怎麼辦。

PHP:

header('Content-Type: text/xml'); 

$Form= array(
    0 => array(
     'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 
     'xsi:noNamespaceSchemaLocation' => 'http://homecredit.net/muchserver/ws/visit/types', 
     'label'=> 'Home Visit Form', 
     'type' => 'TextView', 
     'name' => 'HomeVisitForm' 
    ), 
    1 => array(
     'id' => '002', 
     'name' => 'Ijas', 
     'subjects' => array('Science','History','Social') 
    ) 
); 

// creating object of SimpleXMLElement 
$xml_Form = new SimpleXMLElement("<?xml version=\"1.0\"?><Form></Form>"); 
array_to_xml($Form,$xml_Form);     // function call to convert array to xml 
print $xml_Form->asXML();        //saving generated xml file 


// function defination to convert array to xml 
function array_to_xml($Form, &$xml_Form) { 
    foreach($Form as $key => $value) { 
     $key = is_numeric($key) ? "Field$key" : $key; 
      (is_array($value))    
       ? array_to_xml($value, $xml_Form->addChild("$key")) 
       : $xml_Form->addChild("$key","$value"); 
    } 
} 

?> 

輸出上面的PHP代碼:

<Form> 
    <Field0> 
     <xsi>http://www.w3.org/2001/XMLSchema-instance</xsi> 
     <noNamespaceSchemaLocation>url</noNamespaceSchemaLocation> 
     <label>Home Visit Form</label> 
     <type>TextView</type> 
     <name>HomeVisitForm</name> 
    </Field0> 
    <Field1> 
     <id>002</id> 
     <name>Ijas</name> 
     <subjects> 
      <Field0>Science</Field0> 
      <Field1>History</Field1> 
      <Field2>Social</Field2> 
     </subjects> 
    </Field1> 
</Form> 

回答

0

嗨,你可能需要查看該位

(is_array($value))    
      ? array_to_xml($value, $xml_Form->addChild("$key")) 
      : $xml_Form->addChild("$key","$value"); 

這可能是正確的,但目前還不清楚它的寫法。通常人們會賦值等

$var = (is_array($value))    
      ? array_to_xml($value, $xml_Form->addChild("$key")) 
      : $xml_Form->addChild("$key","$value"); 

或者

if(is_array($value)){   
    array_to_xml($value, $xml_Form->addChild("$key")); //recursive 
}else{ 
    $xml_Form->addChild("$key","$value"); 
} 

它可能無法幫助你,但只是看起來對我不好。此外,但更重要的是,您正試圖設置屬性name="DueDate"爲例。但是,在代碼中的任何地方我都看不到$xml_Form->addAttribute()的調用。

http://php.net/manual/en/simplexmlelement.addattribute.php

可能你需要這樣的事情,但我現在不能測試。

foreach($Form as $key => $value) { 
    $key = is_numeric($key) ? "Field$key" : $key; 
//.... 

將上面的內容更改爲這樣的內容。

$child = $xml_Form->addChild("Field"); 
foreach($Form as $key => $value) { 
    $child->addAttribute($key, $value); 
} 

可能需要一些工作弄清楚如何處理這部分雖然

'subjects' => array('Science','History','Social') 
+0

您好我有一個錯誤PHP的警告做:的SimpleXMLElement ::的addAttribute()預計參數2爲字符串。在'科目'=>數組('科學','歷史','社會')我認爲這是一個領域的孩子 – user4212650 2014-11-04 07:38:28

+0

我想我提到需要一些計算工作。對不起,這裏晚了,我很困。 – ArtisticPhoenix 2014-11-04 08:16:27