stdclass object
(
[out] =>
t
)
以前做这直接读取 xml的,可这种带了一个 stdclass object( [out] =>
不知道应该如何读到各节点内容了
回复讨论(解决方案) 红色代码 是由 print_r($result);得到的, 如果换成echo $result;就会出错 catchable fatal error: object of class stdclass could not be converted to string in
如果你对对象操作不熟悉,可以先转换为数组,这样会方便一些。
你获取的对象很简单, $result->out 就是这个xml字符串,若是想提取其中的内容,可以尝试字符串提取,或者解析xml为对象再转数组(推荐)。
http://www.php.net/manual/zh/language.oop5.properties.php
直接打印
echo $result->out;
作为xml解析
$xml = simplexml_load_string($result->out);
print_r($xml);
simplexmlelement object( [flights] => simplexmlelement object ( [flight] => simplexmlelement object ( [@attributes] => array ( [orgcity] => sha [departterm] => t2 [dstcity] => can [arrivalterm] => -- [aircomp] => mu [flightno] => mu5307 [planetype] => 320 [starttime] => 2012-07-20 07:30 [endtime] => 2013-07-20 09:50 [stopnumber] => 0 [mealcode] => s ) [cabins] => simplexmlelement object ( [0] => simplexmlelement object ( [@attributes] => array ( [cabincode] => y [cabintype] => 0 [cabindiscount] => 100 [cabinname] => 经济舱 [cabinsales] => a [ibeprice] => 1280 [encryptstring] => 79fccacuigjjdskf ) ) ) [cabin] => simplexmlelement object ( [@attributes] => array ( [cabincode] => a [cabintype] => 0 [cabindiscount] => 250 [cabinname] => 头等折扣舱 [cabinsales] => a [ibeprice] => 2830 [encryptstring] => 1c9cc629e7f7b1a ) ) ) ) [is_success] => t)
如果你对对象操作不熟悉,可以先转换为数组,这样会方便一些。
你获取的对象很简单, $result->out 就是这个xml字符串,若是想提取其中的内容,可以尝试字符串提取,或者解析xml为对象再转数组(推荐)。
php code?1http://www.php.net/manual/zh/language.oop5.properties.php
请问如何解析xml为对象再转数组
用domdocument的方法来解析吧
$xml = simplexml_load_string($result->out);// 转换对象所有属性为数组function obj2arr($obj){ $arr = get_object_vars($obj); foreach($arr as &$child){ is_object($child) && ( $child = obj2arr($child) ); } return $arr;}$data = obj2arr($xml);
接着你就可以用
$data['flights']['flight']['cabin']['@attributes']['cabinname'];
得到 头等折扣舱
当然,不转换为数组也是可以直接操作的,不过这就需要考验你的基本功
php code?1234567891011$xml = simplexml_load_string($result->out);// 转换对象所有属性为数组function
显示这一句出错:
simplexml_load_string() [function.simplexml-load-string]: input conversion failed due to input error
你的 php 小于 5.3
只能识别 utf-8 的 xml
所以报错
有什么办法没?
$result->out = str_replace('encoding=gb2312', 'encoding=utf-8', iconv('gbk', 'utf-8', $result->out));
echo $data['flights']['flight']['cabin']['@attributes']['cabinname'];
显示不出来任何数据,怎么回事
