2012-01-05 34 views
0

我正在將一些錯誤管理構建到我的腳本中,並且需要關於如何使其正常工作的一些建議。在XML中迴應數組的特定部分

我很困惑如何實際輸出我的消息到結果函數。我的腳本從一個IF語句開始,如果有問題,它會從我的錯誤函數中選擇一個錯誤。 (我將如何具體選擇哪些錯誤?)

於是開始與我有:

if ($_GET) 
{ 

    // run function 

} else { 

    return $this->error(); 

} 

現在的誤差函數:

private function error($errnum=1000) { 
$data = array(
    'error' => array(
    '1000' => 'Required parameter is missing', 
    '1100' => 'Parameter not recognized', 
    '2000' => 'Currency type not recognized', 
    '2100' => 'Currency amount must be to 2 decimal places', 
    '3000' => 'Service currently unavailable', 
    '3100' => 'Error in service' 
) 
); 
$this->result($data); 
} 

最後結果函數:

private function result($data=array(),$type='XML') { 
    switch(strtolower($type)) {  
     case 'xml': 

      header("Content-type: text/html"); // Set header type to XML 
      $output = new SimpleXMLElement('<conv/>'); // Convert our php array to simpleXML 
      array_walk_recursive($data, array ($output, 'addChild')); 
      echo $output->asXML(); 

     break; 
    } 
    exit; 
} 
+0

我不確定我是否理解..但是您可以選擇錯誤但您想要。 – 2012-01-05 17:20:21

+0

是的,但我不知道如何做到這一點。 (我對PHP非常陌生,所以所有的東西都讓我感到困惑) – tctc91 2012-01-05 17:22:18

+0

如果你想使用錯誤1100,你可以調用'$ this-> error(1100);' – 2012-01-05 17:23:22

回答

0

正是這條線導致問題:

$ this-> result($ data);我不得不將數據變成數組。