使用PHP解析带有递增标记值的XML
我正在使用一些api,它以XML格式返回用户完成的事务列表。这就是它的样子使用PHP解析带有递增标记值的XML,php,xml,Php,Xml,我正在使用一些api,它以XML格式返回用户完成的事务列表。这就是它的样子 <Response> <Status>00</Status> <STMT> <T0> <ID>25624</ID> <DATE>30 JUNE 2014</DATE> <Amount>1500</Amount> </T0>
<Response>
<Status>00</Status>
<STMT>
<T0>
<ID>25624</ID>
<DATE>30 JUNE 2014</DATE>
<Amount>1500</Amount>
</T0>
<T1>
<ID>11495</ID>
<DATE>29 JUNE 2014</DATE>
<Amount>1000</Amount>
</T1>
<T2>
----
----
----
</STMT>
<Bal>55</Bal>
</Response>
请帮忙。实际上这很简单。这样做:
$xml_string = '<Response><Status>00</Status><STMT> <T0> <ID>25624</ID> <DATE>30 JUNE 2014</DATE> <Amount>1500</Amount> </T0> <T1> <ID>11495</ID> <DATE>29 JUNE 2014</DATE> <Amount>1000</Amount> </T1> <T2> <ID>11496</ID> <DATE>28 JUNE 2014</DATE> <Amount>500</Amount> </T2></STMT><Bal>55</Bal></Response>';
$xml = simplexml_load_string($xml_string);
$stmt = $xml->STMT;
$stmt = json_decode(json_encode($stmt), true);
echo '<pre>';
print_r($stmt);
它起作用了。你没有回应结果
此外,
$i++
应改为++$i
“但没有起作用”不符合具体问题的条件。你至少需要更清楚什么不起作用。你以为会发生什么?发生了什么事?你有什么错误吗?等等,pp.(写这篇文章是因为你的代码看起来并不遥远,我想它实际上是可行的)好主意!非常感谢你。
$xml_string = '<Response><Status>00</Status><STMT> <T0> <ID>25624</ID> <DATE>30 JUNE 2014</DATE> <Amount>1500</Amount> </T0> <T1> <ID>11495</ID> <DATE>29 JUNE 2014</DATE> <Amount>1000</Amount> </T1> <T2> <ID>11496</ID> <DATE>28 JUNE 2014</DATE> <Amount>500</Amount> </T2></STMT><Bal>55</Bal></Response>';
$xml = simplexml_load_string($xml_string);
$stmt = $xml->STMT;
$stmt = json_decode(json_encode($stmt), true);
echo '<pre>';
print_r($stmt);
Array
(
[T0] => Array
(
[ID] => 25624
[DATE] => 30 JUNE 2014
[Amount] => 1500
)
[T1] => Array
(
[ID] => 11495
[DATE] => 29 JUNE 2014
[Amount] => 1000
)
[T2] => Array
(
[ID] => 11496
[DATE] => 28 JUNE 2014
[Amount] => 500
)
)
echo $result->STMT->$tx->ID . "\n";