【发布时间】:2014-04-28 11:54:57
【问题描述】:
我确实从 Web 服务接收 XML 结果,我将其读取为 SimpleXML 元素。现在存在结果可能因 Web 服务提供的配置而异的情况。
重点是:我想在“情况 2”中围绕 SimpleXML 对象包装一个数组,因此在处理后面的数据。一段时间以来,我一直在寻找和思考解决方案,但到目前为止我还没有弄清楚任何事情。
情况1:返回数组内的多个SimpleXML元素
["myConfig"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#41 (5) {
["id"]=>
string(1) "1"
["type"]=>
string(1) "4"
["comment"]=>
string(2) "foobar"
["name"]=>
string(1) "test"
["attribute"]=>
string(5) "value"
}
[1]=>
object(SimpleXMLElement)#42 (5) {
["id"]=>
string(1) "4"
["type"]=>
string(1) "2"
["comment"]=>
string(8) "twobar"
["name"]=>
string(10) "test2"
["attribute"]=>
string(5) "value"
}
}
情况2:Config只包含一个SimpleXML元素,web服务没有返回数组
["myConfig"]=>
object(SimpleXMLElement)#41 (5) {
["id"]=>
string(1) "1"
["type"]=>
string(1) "4"
["comment"]=>
string(2) "foobar"
["name"]=>
string(1) "test"
["attribute"]=>
string(5) "value"
}
情况 2 的目标:我想在服务器端从“情况 2”中创建以下内容,然后继续使用配置数据:
["myConfig"]=>
array(1) {
[0]=>
object(SimpleXMLElement)#41 (5) {
["id"]=>
string(1) "1"
["type"]=>
string(1) "4"
["comment"]=>
string(2) "foobar"
["name"]=>
string(1) "test"
["attribute"]=>
string(5) "value"
}
}
【问题讨论】:
-
检查
myConfig是否是一个数组,如果不是就简单地把它变成一个......