【问题标题】:echoing a specific part of an array in XML在 XML 中回显数组的特定部分
【发布时间】:2012-01-05 17:13:22
【问题描述】:

我正在我的脚本中构建一些错误管理,需要一些关于如何使其正常运行的建议。

我很困惑如何将我的消息实际输出到结果函数中。我的脚本以 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;
}

【问题讨论】:

  • 我不确定我是否理解 .. 你可以随意选择错误。
  • 是的,但我不知道该怎么做。 (我对 PHP 很陌生,所以上面的一切都让我感到困惑)
  • 如果您想使用错误 1100,您可以致电$this-&gt;error(1100);
  • 我试过了,但它什么也没做。我认为我的某个函数中某处有错误
  • 您从未在任何地方实际使用过errnum 参数。

标签: php xml arrays simplexml


【解决方案1】:

正是这一行导致了问题:

$this->result($data);

我不得不将数据变量更改为数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多