因此,我设置了一个 EC2 微型实例来试用,并运行一个 php 页面来提取和迭代一些 xml 数据。我不知道这里发生了什么,但“somefile_2.mov”没有被计算出来。有什么想法吗?
XML
SimpleXMLElement Object
(
[Name] => some name
[Prefix] => SimpleXMLElement Object
(
)
[Marker] => SimpleXMLElement Object
(
)
[MaxKeys] => 1000
[IsTruncated] => false
[Contents] => Array
(
[0] => SimpleXMLElement Object
(
[Key] => somefile_1.mov
[LastModified] => 2014-06-20T10:18:44.000Z
[Size] => 2207119304
)
[1] => SimpleXMLElement Object
(
[Key] => somefile_2.mov
[LastModified] => 2014-06-20T10:27:47.000Z
[Size] => 4456151156
)
[2] => SimpleXMLElement Object
(
[Key] => somefile_3.mov
[LastModified] => 2014-06-20T10:44:21.000Z
[Size] => 9218240
)
)
)
<?php
foreach ($xml->Contents as $item) {
echo $item->Key . " Bytes : " . $item->Size . " (" . $item->Size/2 . ")<br>";
}
?>
输出(EC2)
somefile_1.mov Bytes: 2207119304 (1073741823.5)
somefile_2.mov Bytes: 4456151156 (1073741823.5)
somefile_3.mov Bytes: 9218240 (4609120)
输出(本地)
somefile_1.mov Bytes: 2207119304 (1073741823.5)
somefile_2.mov Bytes: 4456151156 (2228075578)
somefile_3.mov Bytes: 9218240 (4609120)
- 经过进一步调查更新,我发现如果我以标准数组而不是简单 XML 对象的形式复制数据,则微实例上一切正常。
数据格式
<?php
$data[] = array(
'name' => 'Item 1',
'size' => 2207119304
);
$data[] = array(
'name' => 'Item 2',
'size' => 4456151156
);
$data[] = array(
'name' => 'Item 3',
'size' => 20120816
);
?>
<ul>
<?php foreach ($data as $item) { ?>
<li><?php echo $item['name'];?> - <?php echo $item['size']/2;?> / <?php echo $item['size'];?></li>
<?php }; ?>
</ul>
输出(EC2)
Item 1 - 1103559652 / 2207119304
Item 2 - 2228075578 / 4456151156
Item 3 - 10060408 / 20120816
答案1
对我来说,答案并不多,解决方法是将简单 XML 数据对象转换为标准数组:
$xml = simplexml_load_file("data.xml");
$json = json_encode($xml);
$data = json_decode($json,TRUE);