【发布时间】:2015-07-01 06:26:44
【问题描述】:
我正在将支付网关集成到网站中,他们的 API 返回一个 xml 对象,其中嵌套了我需要的值。
SimpleXMLElement Object
(
[form] => SimpleXMLElement Object
(
[input] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => hidden
[name] => SessionStored
[value] => True
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => hidden
[name] => SessionStoredError
[value] =>
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => hidden
[name] => SST
[value] => e19e8abe-a2d6-4ce7
)
)
)
)
)
使用php如何将嵌套属性放入如下格式的关联数组中?
$array['SessionStored'] = 'True'
$array['SessionStoredError'] = ''
$array['SST'] = 'e19e8abe-a2d6-4ce7'
有点乱,但是在网上阅读了其他文章后,我整理了以下内容,这会引发“致命错误:调用成员函数属性()”
$xmlData = simplexml_load_string($result);
$aXml = json_decode( json_encode($xmlData) , 1);
$testArray = $aXml['form']['input'];
for($i = 0; $i < count($testArray); $i++)
{
foreach($testArray[$i]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
}
【问题讨论】:
-
@JimGarrison 我已经添加了我当前的代码
标签: php arrays xml simplexml xml-attribute